/WP7.1/Templates/C#/WPCloud.Mem/WindowsPhoneCloud.Web/WebRole.cs
C# | 129 lines | 87 code | 14 blank | 28 comment | 9 complexity | 215add5e836e4a92923a301aab18f7b7 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.Linq;
- #if ACS
- using System.Security.Permissions;
- using System.Xml.Linq;
- #endif
- using Microsoft.Samples.WindowsPhoneCloud.Web.Infrastructure;
- using Microsoft.WindowsAzure;
- using Microsoft.WindowsAzure.Diagnostics;
- using Microsoft.WindowsAzure.ServiceRuntime;
-
- public class WebRole : RoleEntryPoint
- {
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Justification = "This method initializes the Web role.")]
- public override bool OnStart()
- {
- DiagnosticMonitor.Start("Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString");
-
- // For information on handling configuration changes
- // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.
- RoleEnvironment.Changing += this.RoleEnvironmentChanging;
-
- // 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));
-
- RoleEnvironment.Changed += (sender, arg) =>
- {
- if (arg.Changes.OfType<RoleEnvironmentConfigurationSettingChange>()
- .Any((change) => (change.ConfigurationSettingName == configName)))
- {
- // The corresponding configuration setting has changed, propagate the value
- if (!configSetter(RoleEnvironment.GetConfigurationSettingValue(configName)))
- {
- // In this case, the change to the storage account credentials in the
- // service configuration is significant enough that the role needs to be
- // recycled in order to use the latest settings. (for example, the
- // endpoint has changed)
- RoleEnvironment.RequestRecycle();
- }
- }
- };
- });
-
- #if ACS
- // If no valid WIF settings are found in the Web Role configuration, then the Web Role shouldn't start
- if (!UpdateWifSettings())
- {
- return false;
- }
- #endif
-
- return base.OnStart();
- }
-
- #if ACS
- [EnvironmentPermission(SecurityAction.LinkDemand)]
- private static bool UpdateWifSettings()
- {
- using (var server = new Microsoft.Web.Administration.ServerManager())
- {
- var siteNameFromServiceModel = "Web";
- var siteName = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}_{1}", RoleEnvironment.CurrentRoleInstance.Id, siteNameFromServiceModel);
-
- var configFilePath = string.Format(System.Globalization.CultureInfo.InvariantCulture, @"{0}\Web.config", server.Sites[siteName].Applications[0].VirtualDirectories[0].PhysicalPath);
- var xml = XElement.Load(configFilePath);
- var identityModelService = xml.Element("microsoft.identityModel").Element("service");
-
- if (UpdateAttributeWithRoleSetting(identityModelService.Element("audienceUris").Element("add").Attribute("value"), "realm") &&
- UpdateAttributeWithRoleSetting(identityModelService.Element("issuerTokenResolver").Element("serviceKeys").Element("add").Attribute("serviceName"), "realm") &&
- UpdateAttributeWithRoleSetting(identityModelService.Element("issuerTokenResolver").Element("serviceKeys").Element("add").Attribute("serviceKey"), "serviceKey") &&
- UpdateAttributeWithRoleSetting(identityModelService.Element("issuerNameRegistry").Element("trustedIssuers").Element("add").Attribute("issuerIdentifier"), "trustedIssuersIdentifier") &&
- UpdateAttributeWithRoleSetting(identityModelService.Element("issuerNameRegistry").Element("trustedIssuers").Element("add").Attribute("name"), "trustedIssuerName"))
- {
- xml.Save(configFilePath);
- return true;
- }
-
- return false;
- }
- }
-
- private static bool UpdateAttributeWithRoleSetting(XAttribute attribute, string settingName)
- {
- var settingValue = ConfigReader.GetConfigValue(settingName, false);
- if (!string.IsNullOrEmpty(settingValue))
- {
- attribute.Value = settingValue;
- }
- else if (string.IsNullOrEmpty(attribute.Value))
- {
- return false;
- }
-
- return true;
- }
- #endif
-
- private void RoleEnvironmentChanging(object sender, RoleEnvironmentChangingEventArgs e)
- {
- // If a configuration setting is changing
- if (e.Changes.Any(change => change is RoleEnvironmentConfigurationSettingChange))
- {
- // Set e.Cancel to true to restart this role instance
- e.Cancel = true;
- }
- }
- }
- }