/WP7.1/Templates/C#/WPCloud.ACS/WindowsPhoneCloud.Web/Global.asax.cs
C# | 295 lines | 231 code | 43 blank | 21 comment | 26 complexity | 8f7e21ea64dbd88bd0c385db5c8a4f45 MD5 | raw file
- // ----------------------------------------------------------------------------------
- // Microsoft Developer & Platform Evangelism
- //
- // Copyright (c) Microsoft Corporation. All rights reserved.
- //
- // THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
- // EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
- // OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
- // ----------------------------------------------------------------------------------
- // The example companies, organizations, products, domain names,
- // e-mail addresses, logos, people, places, and events depicted
- // herein are fictitious. No association with any real company,
- // organization, product, domain name, email address, logo, person,
- // places, or events is intended or should be inferred.
- // ----------------------------------------------------------------------------------
-
- namespace Microsoft.Samples.WindowsPhoneCloud.Web
- {
- using System;
- using System.Data.Entity;
- using System.Globalization;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using System.Web.Routing;
- using System.Web.Security;
- using Microsoft.Samples.WindowsPhoneCloud.Web.Controllers;
- using Microsoft.Samples.WindowsPhoneCloud.Web.Infrastructure;
- using Microsoft.Samples.WindowsPhoneCloud.Web.Models;
- using Microsoft.Samples.WindowsPhoneCloud.Web.Services;
- using Microsoft.WindowsAzure;
- using Microsoft.WindowsAzure.ServiceRuntime;
- using Microsoft.WindowsAzure.StorageClient;
-
- public class MvcApplication : System.Web.HttpApplication
- {
- private const int DefaultHttpsPort = 443;
- private const int DefaultHttpPort = 10080;
- private const string PortErrorMessage = @"The Web role was started in a wrong port.
- For this sample application to work correctly, please make sure that it is running in port {0}.
- Please review the Troubleshooting section of the sample documentation for instructions on how to do this.";
-
- private static bool securityInitialized = false;
-
- public static void RegisterGlobalFilters(GlobalFilterCollection filters)
- {
- filters.Add(new HandleErrorAttribute());
- }
-
- public static void RegisterRoutes(RouteCollection routes)
- {
- routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
-
- routes.MapRoute(
- "Default",
- "{controller}/{action}/{id}",
- new { controller = "Home", action = "Index", id = (string)null },
- new { controller = new ListConstraint(ListConstraintType.Exclude, "RegistrationService", "SharedAccessSignatureService", "PushNotificationService", "SqlAzureSampleODataService") });
- }
-
- protected void Application_Start()
- {
- // This code sets up a handler to update CloudStorageAccount instances when their corresponding
- // configuration settings change in the service configuration file.
- CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>
- {
- // Provide the configSetter with the initial value.
- configSetter(RoleEnvironment.GetConfigurationSettingValue(configName));
- });
-
- AreaRegistration.RegisterAllAreas();
-
- RegisterGlobalFilters(GlobalFilters.Filters);
- RegisterRoutes(RouteTable.Routes);
-
- RouteTable.Routes.AddWcfServiceRoute<RegistrationService>("RegistrationService");
- RouteTable.Routes.AddWcfServiceRoute<SharedAccessSignatureService>("SharedAccessSignatureService");
- RouteTable.Routes.AddWcfServiceRoute<SamplePushUserRegistrationService>("PushNotificationService");
- RouteTable.Routes.AddWcfServiceRoute<SqlAzureSampleODataService>("SqlAzureSampleODataService");
-
- var account = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
- CreateSilverlightClientAccessPolicy(account.CreateCloudBlobClient());
- CreateCloudTables(account.CreateCloudTableClient());
-
- Database.SetInitializer<SqlSampleDataContext>(new SqlSampleDataInitializer());
- }
-
- protected void Session_Start(object sender, EventArgs e)
- {
- if (!securityInitialized)
- {
- InitializeSecurity();
- securityInitialized = true;
- }
- }
-
- protected void Application_BeginRequest(object sender, EventArgs e)
- {
- if (this.ShouldRedirectToHttps())
- {
- this.RedirectScheme(this.Context.Request.Url, "https");
- }
- else if (this.ShouldRedirectToHttp())
- {
- this.RedirectScheme(this.Context.Request.Url, "http");
- }
-
- if (!this.IsPortNumberOK() && !IsAllowedContent(this.Context.Request.Path))
- {
- this.CreateWrongPortException();
- }
- }
-
- private static void InitializeSecurity()
- {
- var adminUser = Membership.FindUsersByName("admin").Cast<MembershipUser>().FirstOrDefault();
- if (adminUser == null)
- {
- adminUser = Membership.CreateUser("admin", "Passw0rd!", "admin@wp7cloudapp.com");
- }
-
- var adminUserId = adminUser.ProviderUserKey.ToString();
- IUserPrivilegesRepository userPrivilegesRepository = new UserTablesServiceContext();
- userPrivilegesRepository.AddPrivilegeToUser(adminUserId, PrivilegeConstants.AdminPrivilege);
- }
-
- private static bool IsAllowedContent(string path)
- {
- return path.EndsWith("/Error", StringComparison.OrdinalIgnoreCase)
- || path.StartsWith("/Content", StringComparison.OrdinalIgnoreCase)
- || path.StartsWith("/Scripts", StringComparison.OrdinalIgnoreCase);
- }
-
- private static void CreateSilverlightClientAccessPolicy(CloudBlobClient cloudBlobClient)
- {
- var container = cloudBlobClient.GetContainerReference("$root");
- container.CreateIfNotExist();
- container.SetPermissions(
- new BlobContainerPermissions
- {
- PublicAccess = BlobContainerPublicAccessType.Blob
- });
-
- var blob = cloudBlobClient.GetBlobReference("clientaccesspolicy.xml");
- blob.Properties.ContentType = "text/xml";
- blob.UploadText(
- @"<?xml version=""1.0"" encoding=""utf-8""?>
- <access-policy>
- <cross-domain-access>
- <policy>
- <allow-from http-methods=""*"" http-request-headers=""*"">
- <domain uri=""*"" />
- <domain uri=""http://*"" />
- </allow-from>
- <grant-to>
- <resource path=""/"" include-subpaths=""true"" />
- </grant-to>
- </policy>
- </cross-domain-access>
- </access-policy>");
- }
-
- private static void CreateCloudTables(CloudTableClient cloudTableClient)
- {
- CreatePushNotificationTable(cloudTableClient);
- CreateUserTable(cloudTableClient);
- CreateUserPrivilegeTable(cloudTableClient);
- }
-
- private static void CreatePushNotificationTable(CloudTableClient cloudTableClient)
- {
- cloudTableClient.CreateTableIfNotExist(UserTablesServiceContext.PushUserTableName);
-
- // Execute conditionally for development storage only.
- if (cloudTableClient.BaseUri.IsLoopback)
- {
- var context = cloudTableClient.GetDataServiceContext();
- var entity = new PushUserEndpoint("applicationID", "deviceId") { UserId = "UserName", ChannelUri = "http://tempuri", TileCount = 0 };
-
- context.AddObject(UserTablesServiceContext.PushUserTableName, entity);
- context.SaveChangesWithRetries();
- context.DeleteObject(entity);
- context.SaveChangesWithRetries();
- }
- }
-
- private static void CreateUserTable(CloudTableClient cloudTableClient)
- {
- cloudTableClient.CreateTableIfNotExist(UserTablesServiceContext.UserTableName);
-
- // Execute conditionally for development storage only.
- if (cloudTableClient.BaseUri.IsLoopback)
- {
- var context = cloudTableClient.GetDataServiceContext();
- var entity = new User { Name = "UserName", Email = "user@email.com" };
-
- context.AddObject(UserTablesServiceContext.UserTableName, entity);
- context.SaveChangesWithRetries();
- context.DeleteObject(entity);
- context.SaveChangesWithRetries();
- }
- }
-
- private static void CreateUserPrivilegeTable(CloudTableClient cloudTableClient)
- {
- cloudTableClient.CreateTableIfNotExist(UserTablesServiceContext.UserPrivilegeTableName);
-
- // Execute conditionally for development storage only.
- if (cloudTableClient.BaseUri.IsLoopback)
- {
- var context = cloudTableClient.GetDataServiceContext();
- var entity = new UserPrivilege { UserId = "UserId", Privilege = "Privilege" };
-
- context.AddObject(UserTablesServiceContext.UserPrivilegeTableName, entity);
- context.SaveChangesWithRetries();
- context.DeleteObject(entity);
- context.SaveChangesWithRetries();
- }
- }
-
- private void RedirectScheme(Uri originalUri, string intendedScheme)
- {
- int portNumber = 0;
- if (intendedScheme.Equals("https", StringComparison.OrdinalIgnoreCase))
- {
- portNumber = DefaultHttpsPort;
- }
- else if (intendedScheme.Equals("http", StringComparison.OrdinalIgnoreCase))
- {
- portNumber = DefaultHttpPort;
- }
-
- var redirectUrl = string.Format(
- CultureInfo.InvariantCulture,
- "{0}://{1}:{2}{3}",
- intendedScheme,
- originalUri.Host,
- portNumber,
- originalUri.PathAndQuery);
-
- this.Response.Redirect(redirectUrl, true);
- }
-
- private bool ShouldRedirectToHttp()
- {
- return this.Request.IsSecureConnection && this.Context.Request.Url.ToString().EndsWith(".cer", StringComparison.OrdinalIgnoreCase);
- }
-
- private bool ShouldRedirectToHttps()
- {
- return !this.Request.IsSecureConnection && !this.Context.Request.Url.ToString().EndsWith(".cer", StringComparison.OrdinalIgnoreCase);
- }
-
- private void CreateWrongPortException()
- {
- var exception = new RoleInWrongPortException(string.Format(CultureInfo.InvariantCulture, PortErrorMessage, DefaultHttpsPort));
- var routeData = new RouteData();
- routeData.Values.Add("Controller", "Error");
- routeData.Values.Add("Action", "Index");
- routeData.Values.Add("Error", exception);
-
- using (var errorController = new ErrorController())
- {
- ((IController)errorController).Execute(new RequestContext(new HttpContextWrapper(this.Context), routeData));
- }
-
- this.Context.Response.End();
- }
-
- private bool IsPortNumberOK()
- {
- var scheme = this.Context.Request.Url.Scheme;
- var portNumber = 0;
-
- if (scheme.Equals("https"))
- {
- portNumber = DefaultHttpsPort;
- }
- else if (scheme.Equals("http"))
- {
- portNumber = DefaultHttpPort;
- }
-
- var hostAddress = this.Context.Request.Headers["Host"] ?? string.Empty;
- var portPosition = hostAddress.IndexOf(":", StringComparison.OrdinalIgnoreCase);
-
- if (portPosition > 0)
- {
- int.TryParse(hostAddress.Substring(portPosition + 1), out portNumber);
- }
-
- return (portNumber == DefaultHttpsPort) || ((portNumber == DefaultHttpPort) && Context.Request.Url.ToString().EndsWith(".cer", StringComparison.OrdinalIgnoreCase));
- }
- }
- }