PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/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
  1. // ----------------------------------------------------------------------------------
  2. // Microsoft Developer & Platform Evangelism
  3. //
  4. // Copyright (c) Microsoft Corporation. All rights reserved.
  5. //
  6. // THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  7. // EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
  8. // OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  9. // ----------------------------------------------------------------------------------
  10. // The example companies, organizations, products, domain names,
  11. // e-mail addresses, logos, people, places, and events depicted
  12. // herein are fictitious. No association with any real company,
  13. // organization, product, domain name, email address, logo, person,
  14. // places, or events is intended or should be inferred.
  15. // ----------------------------------------------------------------------------------
  16. namespace Microsoft.Samples.WindowsPhoneCloud.Web
  17. {
  18. using System.Linq;
  19. #if ACS
  20. using System.Security.Permissions;
  21. using System.Xml.Linq;
  22. #endif
  23. using Microsoft.Samples.WindowsPhoneCloud.Web.Infrastructure;
  24. using Microsoft.WindowsAzure;
  25. using Microsoft.WindowsAzure.Diagnostics;
  26. using Microsoft.WindowsAzure.ServiceRuntime;
  27. public class WebRole : RoleEntryPoint
  28. {
  29. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Justification = "This method initializes the Web role.")]
  30. public override bool OnStart()
  31. {
  32. DiagnosticMonitor.Start("Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString");
  33. // For information on handling configuration changes
  34. // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.
  35. RoleEnvironment.Changing += this.RoleEnvironmentChanging;
  36. // This code sets up a handler to update CloudStorageAccount instances when their corresponding
  37. // configuration settings change in the service configuration file.
  38. CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>
  39. {
  40. // Provide the configSetter with the initial value
  41. configSetter(RoleEnvironment.GetConfigurationSettingValue(configName));
  42. RoleEnvironment.Changed += (sender, arg) =>
  43. {
  44. if (arg.Changes.OfType<RoleEnvironmentConfigurationSettingChange>()
  45. .Any((change) => (change.ConfigurationSettingName == configName)))
  46. {
  47. // The corresponding configuration setting has changed, propagate the value
  48. if (!configSetter(RoleEnvironment.GetConfigurationSettingValue(configName)))
  49. {
  50. // In this case, the change to the storage account credentials in the
  51. // service configuration is significant enough that the role needs to be
  52. // recycled in order to use the latest settings. (for example, the
  53. // endpoint has changed)
  54. RoleEnvironment.RequestRecycle();
  55. }
  56. }
  57. };
  58. });
  59. #if ACS
  60. // If no valid WIF settings are found in the Web Role configuration, then the Web Role shouldn't start
  61. if (!UpdateWifSettings())
  62. {
  63. return false;
  64. }
  65. #endif
  66. return base.OnStart();
  67. }
  68. #if ACS
  69. [EnvironmentPermission(SecurityAction.LinkDemand)]
  70. private static bool UpdateWifSettings()
  71. {
  72. using (var server = new Microsoft.Web.Administration.ServerManager())
  73. {
  74. var siteNameFromServiceModel = "Web";
  75. var siteName = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}_{1}", RoleEnvironment.CurrentRoleInstance.Id, siteNameFromServiceModel);
  76. var configFilePath = string.Format(System.Globalization.CultureInfo.InvariantCulture, @"{0}\Web.config", server.Sites[siteName].Applications[0].VirtualDirectories[0].PhysicalPath);
  77. var xml = XElement.Load(configFilePath);
  78. var identityModelService = xml.Element("microsoft.identityModel").Element("service");
  79. if (UpdateAttributeWithRoleSetting(identityModelService.Element("audienceUris").Element("add").Attribute("value"), "realm") &&
  80. UpdateAttributeWithRoleSetting(identityModelService.Element("issuerTokenResolver").Element("serviceKeys").Element("add").Attribute("serviceName"), "realm") &&
  81. UpdateAttributeWithRoleSetting(identityModelService.Element("issuerTokenResolver").Element("serviceKeys").Element("add").Attribute("serviceKey"), "serviceKey") &&
  82. UpdateAttributeWithRoleSetting(identityModelService.Element("issuerNameRegistry").Element("trustedIssuers").Element("add").Attribute("issuerIdentifier"), "trustedIssuersIdentifier") &&
  83. UpdateAttributeWithRoleSetting(identityModelService.Element("issuerNameRegistry").Element("trustedIssuers").Element("add").Attribute("name"), "trustedIssuerName"))
  84. {
  85. xml.Save(configFilePath);
  86. return true;
  87. }
  88. return false;
  89. }
  90. }
  91. private static bool UpdateAttributeWithRoleSetting(XAttribute attribute, string settingName)
  92. {
  93. var settingValue = ConfigReader.GetConfigValue(settingName, false);
  94. if (!string.IsNullOrEmpty(settingValue))
  95. {
  96. attribute.Value = settingValue;
  97. }
  98. else if (string.IsNullOrEmpty(attribute.Value))
  99. {
  100. return false;
  101. }
  102. return true;
  103. }
  104. #endif
  105. private void RoleEnvironmentChanging(object sender, RoleEnvironmentChangingEventArgs e)
  106. {
  107. // If a configuration setting is changing
  108. if (e.Changes.Any(change => change is RoleEnvironmentConfigurationSettingChange))
  109. {
  110. // Set e.Cancel to true to restart this role instance
  111. e.Cancel = true;
  112. }
  113. }
  114. }
  115. }