RoleManagerModule.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // System.Web.Security.RoleManagerModule
  3. //
  4. // Authors:
  5. // Ben Maurer ([email protected])
  6. //
  7. // (C) 2003 Ben Maurer
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. using System.Collections;
  31. using System.Collections.Specialized;
  32. using System.Security.Principal;
  33. using System.Text;
  34. using System.Threading;
  35. using System.Web.Configuration;
  36. namespace System.Web.Security {
  37. public sealed class RoleManagerModule : IHttpModule {
  38. RoleManagerSection _config = null;
  39. public event RoleManagerEventHandler GetRoles;
  40. public void Dispose ()
  41. {
  42. }
  43. void ClearCookie (HttpApplication app, string cookieName)
  44. {
  45. HttpCookie clearCookie = new HttpCookie (_config.CookieName, "");
  46. clearCookie.Path = _config.CookiePath;
  47. clearCookie.Expires = DateTime.MinValue;
  48. clearCookie.Domain = _config.Domain;
  49. clearCookie.Secure = _config.CookieRequireSSL;
  50. app.Response.SetCookie (clearCookie);
  51. }
  52. void OnPostAuthenticateRequest (object sender, EventArgs args)
  53. {
  54. HttpApplication app = (HttpApplication)sender;
  55. /* if we're disabled, bail out early */
  56. if (!_config.Enabled)
  57. return;
  58. /* allow the user to populate the Role */
  59. if (GetRoles != null) {
  60. RoleManagerEventArgs role_args = new RoleManagerEventArgs (app.Context);
  61. GetRoles (this, role_args);
  62. if (role_args.RolesPopulated)
  63. return;
  64. }
  65. RolePrincipal principal;
  66. HttpCookie cookie = app.Request.Cookies [_config.CookieName];
  67. IIdentity currentIdentity = app.Context.User.Identity;
  68. if (app.Request.IsAuthenticated) {
  69. if (cookie != null) {
  70. if (!_config.CacheRolesInCookie)
  71. cookie = null;
  72. else if (_config.CookieRequireSSL && !app.Request.IsSecureConnection) {
  73. cookie = null;
  74. ClearCookie (app, _config.CookieName);
  75. }
  76. }
  77. if (cookie == null)
  78. principal = new RolePrincipal (currentIdentity);
  79. else
  80. principal = new RolePrincipal (currentIdentity, cookie.Value);
  81. }
  82. else {
  83. /* anonymous request */
  84. if (cookie != null) {
  85. ClearCookie (app, _config.CookieName);
  86. }
  87. principal = new RolePrincipal (currentIdentity);
  88. }
  89. app.Context.User = principal;
  90. Thread.CurrentPrincipal = principal;
  91. }
  92. void OnEndRequest (object sender, EventArgs args)
  93. {
  94. HttpApplication app = (HttpApplication)sender;
  95. /* if we're not enabled or configured to cache
  96. * cookies, bail out */
  97. if (!_config.Enabled || !_config.CacheRolesInCookie)
  98. return;
  99. /* if the user isn't authenticated, bail
  100. * out */
  101. if (!app.Request.IsAuthenticated)
  102. return;
  103. /* if the configuration requires ssl for
  104. * cookies and we're not on an ssl connection,
  105. * bail out */
  106. if (_config.CookieRequireSSL && !app.Request.IsSecureConnection)
  107. return;
  108. RolePrincipal principal = app.Context.User as RolePrincipal;
  109. if (principal == null) /* just for my sanity */
  110. return;
  111. if (!principal.CachedListChanged)
  112. return;
  113. string ticket = principal.ToEncryptedTicket ();
  114. if (ticket == null || ticket.Length > 4096) {
  115. ClearCookie (app, _config.CookieName);
  116. return;
  117. }
  118. HttpCookie cookie = new HttpCookie (_config.CookieName, ticket);
  119. cookie.HttpOnly = true;
  120. if (!string.IsNullOrEmpty (_config.Domain))
  121. cookie.Domain = _config.Domain;
  122. if (_config.CookieRequireSSL)
  123. cookie.Secure = true;
  124. if (_config.CookiePath.Length > 1) // more than '/'
  125. cookie.Path = _config.CookiePath;
  126. app.Response.SetCookie (cookie);
  127. }
  128. public void Init (HttpApplication app)
  129. {
  130. app.PostAuthenticateRequest += OnPostAuthenticateRequest;
  131. app.EndRequest += OnEndRequest;
  132. _config = (RoleManagerSection) WebConfigurationManager.GetSection ("system.web/roleManager");
  133. }
  134. }
  135. }
  136. #endif