PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/WP7.1/BabelCam/C#/BabelCam.Web/WebRole.cs

#
C# | 130 lines | 88 code | 14 blank | 28 comment | 9 complexity | d0613d7866fc1b0aeb5aafdb30d6741d 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.BabelCam.Web
  17. {
  18. using System;
  19. using System.Diagnostics;
  20. using System.Linq;
  21. using System.Security.Permissions;
  22. using System.Xml.Linq;
  23. using Microsoft.Samples.BabelCam.Infrastructure.Helpers;
  24. using Microsoft.Samples.BabelCam.Infrastructure.Models;
  25. using Microsoft.Samples.BabelCam.Web.Helpers;
  26. using Microsoft.Samples.BabelCam.Web.Models;
  27. using Microsoft.Samples.BabelCam.Web.Services;
  28. using Microsoft.WindowsAzure;
  29. using Microsoft.WindowsAzure.Diagnostics;
  30. using Microsoft.WindowsAzure.ServiceRuntime;
  31. using Microsoft.WindowsAzure.StorageClient;
  32. public class WebRole : RoleEntryPoint
  33. {
  34. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Justification = "This method initializes the Web role.")]
  35. public override bool OnStart()
  36. {
  37. DiagnosticMonitor.Start("Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString");
  38. // For information on handling configuration changes
  39. // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.
  40. RoleEnvironment.Changing += this.RoleEnvironmentChanging;
  41. // This code sets up a handler to update CloudStorageAccount instances when their corresponding
  42. // configuration settings change in the service configuration file.
  43. CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>
  44. {
  45. // Provide the configSetter with the initial value
  46. configSetter(RoleEnvironment.GetConfigurationSettingValue(configName));
  47. RoleEnvironment.Changed += (sender, arg) =>
  48. {
  49. if (arg.Changes.OfType<RoleEnvironmentConfigurationSettingChange>()
  50. .Any((change) => (change.ConfigurationSettingName == configName)))
  51. {
  52. // The corresponding configuration setting has changed, propagate the value
  53. if (!configSetter(RoleEnvironment.GetConfigurationSettingValue(configName)))
  54. {
  55. // In this case, the change to the storage account credentials in the
  56. // service configuration is significant enough that the role needs to be
  57. // recycled in order to use the latest settings. (for example, the
  58. // endpoint has changed)
  59. RoleEnvironment.RequestRecycle();
  60. }
  61. }
  62. };
  63. });
  64. // If no valid WIF settings are found in the Web Role configuration, then the Web Role shouldn't start
  65. if (!UpdateWifSettings())
  66. {
  67. return false;
  68. }
  69. return base.OnStart();
  70. }
  71. [EnvironmentPermission(SecurityAction.LinkDemand)]
  72. private static bool UpdateWifSettings()
  73. {
  74. using (var server = new Microsoft.Web.Administration.ServerManager())
  75. {
  76. var siteNameFromServiceModel = "Web";
  77. var siteName = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}_{1}", RoleEnvironment.CurrentRoleInstance.Id, siteNameFromServiceModel);
  78. var configFilePath = string.Format(System.Globalization.CultureInfo.InvariantCulture, @"{0}\Web.config", server.Sites[siteName].Applications[0].VirtualDirectories[0].PhysicalPath);
  79. var xml = XElement.Load(configFilePath);
  80. var identityModelService = xml.Element("microsoft.identityModel").Element("service");
  81. if (UpdateAttributeWithRoleSetting(identityModelService.Element("audienceUris").Element("add").Attribute("value"), "realm") &&
  82. UpdateAttributeWithRoleSetting(identityModelService.Element("issuerTokenResolver").Element("serviceKeys").Element("add").Attribute("serviceName"), "realm") &&
  83. UpdateAttributeWithRoleSetting(identityModelService.Element("issuerTokenResolver").Element("serviceKeys").Element("add").Attribute("serviceKey"), "serviceKey") &&
  84. UpdateAttributeWithRoleSetting(identityModelService.Element("issuerNameRegistry").Element("trustedIssuers").Element("add").Attribute("issuerIdentifier"), "trustedIssuersIdentifier") &&
  85. UpdateAttributeWithRoleSetting(identityModelService.Element("issuerNameRegistry").Element("trustedIssuers").Element("add").Attribute("name"), "trustedIssuerName"))
  86. {
  87. xml.Save(configFilePath);
  88. return true;
  89. }
  90. return false;
  91. }
  92. }
  93. private static bool UpdateAttributeWithRoleSetting(XAttribute attribute, string settingName)
  94. {
  95. var settingValue = ConfigReader.GetConfigValue(settingName, false);
  96. if (!string.IsNullOrEmpty(settingValue))
  97. {
  98. attribute.Value = settingValue;
  99. }
  100. else if (string.IsNullOrEmpty(attribute.Value))
  101. {
  102. return false;
  103. }
  104. return true;
  105. }
  106. private void RoleEnvironmentChanging(object sender, RoleEnvironmentChangingEventArgs e)
  107. {
  108. // If a configuration setting is changing
  109. if (e.Changes.Any(change => change is RoleEnvironmentConfigurationSettingChange))
  110. {
  111. // Set e.Cancel to true to restart this role instance
  112. e.Cancel = true;
  113. }
  114. }
  115. }
  116. }