SystemWebHelper.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Activation
  5. {
  6. using System.Reflection;
  7. using System.Runtime;
  8. using System.Runtime.CompilerServices;
  9. using System.ServiceModel;
  10. using System.Web.Security;
  11. // Note: The helper provides utility in safe access of System.Web methods for both client and extended SKUs.
  12. static class SystemWebHelper
  13. {
  14. static Type typeOfRoles;
  15. static Type typeOfMembership;
  16. static Type typeOfWebContext;
  17. static bool defaultRoleProviderSet;
  18. static RoleProvider defaultRoleProvider;
  19. static Type TypeOfRoles
  20. {
  21. get
  22. {
  23. if (typeOfRoles == null)
  24. {
  25. typeOfRoles = GetSystemWebType("System.Web.Security.Roles");
  26. }
  27. return typeOfRoles;
  28. }
  29. }
  30. static Type TypeOfMembership
  31. {
  32. get
  33. {
  34. if (typeOfMembership == null)
  35. {
  36. typeOfMembership = GetSystemWebType("System.Web.Security.Membership");
  37. }
  38. return typeOfMembership;
  39. }
  40. }
  41. static Type TypeOfWebContext
  42. {
  43. get
  44. {
  45. if (typeOfWebContext == null)
  46. {
  47. typeOfWebContext = GetSystemWebType("System.Web.Configuration.WebContext");
  48. }
  49. return typeOfWebContext;
  50. }
  51. }
  52. static Type GetSystemWebType(string typeName)
  53. {
  54. #pragma warning disable 436
  55. return Type.GetType(typeName + ", " + AssemblyRef.SystemWeb, false);
  56. #pragma warning restore 436
  57. }
  58. // Invoke for System.Web.Security.Roles.Enabled ? System.Web.Security.Roles.Provider : null
  59. internal static RoleProvider GetDefaultRoleProvider()
  60. {
  61. if (defaultRoleProviderSet)
  62. {
  63. return defaultRoleProvider;
  64. }
  65. Type roleType = TypeOfRoles;
  66. RoleProvider result = null;
  67. if (roleType != null)
  68. {
  69. // Running on extended sku
  70. try
  71. {
  72. PropertyInfo rolesEnabledPropertyInfo = roleType.GetProperty("Enabled");
  73. Fx.Assert(rolesEnabledPropertyInfo != null, "rolesEnabledPropertyInfo must not be null!");
  74. if (((bool)rolesEnabledPropertyInfo.GetValue(null, null)) == true)
  75. {
  76. PropertyInfo rolesProviderPropertyInfo = roleType.GetProperty("Provider");
  77. Fx.Assert(rolesProviderPropertyInfo != null, "rolesProviderPropertyInfo must not be null!");
  78. result = rolesProviderPropertyInfo.GetValue(null, null) as RoleProvider;
  79. }
  80. }
  81. catch (TargetInvocationException exception)
  82. {
  83. // Since reflection invoke always throws TargetInvocationException,
  84. // we need to (best effort) maintain the exception contract by rethrow inner exception.
  85. if (exception.InnerException != null)
  86. {
  87. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(exception.InnerException);
  88. }
  89. throw;
  90. }
  91. }
  92. else
  93. {
  94. // Running in client SKU
  95. // This is consistent with no provider
  96. }
  97. defaultRoleProvider = result;
  98. defaultRoleProviderSet = true;
  99. return result;
  100. }
  101. // Invoke for System.Web.Security.Roles.Providers[roleProviderName]
  102. internal static RoleProvider GetRoleProvider(string roleProviderName)
  103. {
  104. Type roleType = TypeOfRoles;
  105. if (roleType != null)
  106. {
  107. try
  108. {
  109. // Running on extended sku
  110. PropertyInfo roleProvidersPropertyInfo = roleType.GetProperty("Providers");
  111. Fx.Assert(roleProvidersPropertyInfo != null, "roleProvidersPropertyInfo must not be null!");
  112. // This could throw if RoleManager is not enabled.
  113. object roleProviderCollection = roleProvidersPropertyInfo.GetValue(null, null);
  114. Fx.Assert(roleProviderCollection != null, "roleProviderCollection must not be null!");
  115. PropertyInfo itemPropertyInfo = roleProviderCollection.GetType().GetProperty("Item", new Type[] { typeof(string) });
  116. Fx.Assert(itemPropertyInfo != null, "itemPropertyInfo must not be null!");
  117. return (RoleProvider)itemPropertyInfo.GetValue(roleProviderCollection, new object[] { roleProviderName });
  118. }
  119. catch (TargetInvocationException exception)
  120. {
  121. // Since reflection invoke always throws TargetInvocationException,
  122. // we need to (best effort) maintain the exception contract by rethrow inner exception.
  123. if (exception.InnerException != null)
  124. {
  125. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(exception.InnerException);
  126. }
  127. throw;
  128. }
  129. }
  130. // Running in client SKU
  131. // This is consistent with no provider is a given name.
  132. return null;
  133. }
  134. // Invoke for System.Web.Security.Membership.Provider
  135. internal static MembershipProvider GetMembershipProvider()
  136. {
  137. Type membershipType = TypeOfMembership;
  138. if (membershipType != null)
  139. {
  140. try
  141. {
  142. PropertyInfo membershipProviderPropertyInfo = membershipType.GetProperty("Provider");
  143. Fx.Assert(membershipProviderPropertyInfo != null, "membershipProviderPropertyInfo must not be null!");
  144. return (MembershipProvider)membershipProviderPropertyInfo.GetValue(null, null);
  145. }
  146. catch (TargetInvocationException exception)
  147. {
  148. // Since reflection invoke always throws TargetInvocationException,
  149. // we need to (best effort) maintain the exception contract by rethrow inner exception.
  150. if (exception.InnerException != null)
  151. {
  152. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(exception.InnerException);
  153. }
  154. throw;
  155. }
  156. }
  157. // Running in client SKU
  158. // This is consistent with no default provider.
  159. return null;
  160. }
  161. // Invoke for System.Web.Security.Membership.Providers[membershipProviderName]
  162. internal static MembershipProvider GetMembershipProvider(string membershipProviderName)
  163. {
  164. Type membershipType = TypeOfMembership;
  165. if (membershipType != null)
  166. {
  167. try
  168. {
  169. PropertyInfo membershipProvidersPropertyInfo = membershipType.GetProperty("Providers");
  170. Fx.Assert(membershipProvidersPropertyInfo != null, "membershipProvidersPropertyInfo must not be null!");
  171. object membershipProviderCollection = membershipProvidersPropertyInfo.GetValue(null, null);
  172. Fx.Assert(membershipProviderCollection != null, "membershipProviderCollection must not be null!");
  173. PropertyInfo itemPropertyInfo = membershipProviderCollection.GetType().GetProperty("Item", new Type[] { typeof(string) });
  174. Fx.Assert(itemPropertyInfo != null, "itemPropertyInfo must not be null!");
  175. return (MembershipProvider)itemPropertyInfo.GetValue(membershipProviderCollection, new object[] { membershipProviderName });
  176. }
  177. catch (TargetInvocationException exception)
  178. {
  179. // Since reflection invoke always throws TargetInvocationException,
  180. // we need to (best effort) maintain the exception contract by rethrow inner exception.
  181. if (exception.InnerException != null)
  182. {
  183. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(exception.InnerException);
  184. }
  185. throw;
  186. }
  187. }
  188. // Running in client SKU
  189. // This is consistent with no provider is a given name.
  190. return null;
  191. }
  192. // check if ((System.Web.Configuration.WebContext)configHostingContext).ApplicationLevel == WebApplicationLevel.AboveApplication
  193. internal static bool IsWebConfigAboveApplication(object configHostingContext)
  194. {
  195. Type webContextType = TypeOfWebContext;
  196. if (configHostingContext == null
  197. || webContextType == null
  198. || configHostingContext.GetType() != webContextType)
  199. {
  200. // if we don't recognize the context we can't enforce the special web.config logic
  201. return false;
  202. }
  203. const int webApplicationLevelAboveApplication = 10; // public value of the enum
  204. Fx.Assert(GetSystemWebType("System.Web.Configuration.WebApplicationLevel") != null, "Type 'System.Web.Configuration.WebApplicationLevel' MUST exist in System.Web.dll.");
  205. Fx.Assert(GetSystemWebType("System.Web.Configuration.WebApplicationLevel").GetProperty("AboveApplication") == null ||
  206. (int)GetSystemWebType("System.Web.Configuration.WebApplicationLevel").GetProperty("AboveApplication").GetValue(null, null) == webApplicationLevelAboveApplication,
  207. "unexpected property value");
  208. try
  209. {
  210. PropertyInfo applicationLevelPropertyInfo = webContextType.GetProperty("ApplicationLevel");
  211. Fx.Assert(applicationLevelPropertyInfo != null, "applicationLevelPropertyInfo must not be null!");
  212. return (int)applicationLevelPropertyInfo.GetValue(configHostingContext, null) == webApplicationLevelAboveApplication;
  213. }
  214. catch (TargetInvocationException exception)
  215. {
  216. // Since reflection invoke always throws TargetInvocationException,
  217. // we need to (best effort) maintain the exception contract by rethrow inner exception.
  218. if (exception.InnerException != null)
  219. {
  220. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(exception.InnerException);
  221. }
  222. throw;
  223. }
  224. }
  225. }
  226. }