RoleManagerModule.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. public event RoleManagerEventHandler GetRoles;
  39. public void Dispose ()
  40. {
  41. }
  42. void ClearCookie (HttpApplication app, string cookieName)
  43. {
  44. new HttpCookie (cookieName, "", "", DateTime.MinValue);
  45. }
  46. void OnPostAuthenticateRequest (object sender, EventArgs args)
  47. {
  48. HttpApplication app = (HttpApplication)sender;
  49. RoleManagerSection config = (RoleManagerSection)WebConfigurationManager.GetSection ("system.web/roleManager");
  50. /* if we're disabled, bail out early */
  51. if (!config.Enabled)
  52. return;
  53. /* allow the user to populate the Role */
  54. if (GetRoles != null) {
  55. RoleManagerEventArgs role_args = new RoleManagerEventArgs (app.Context);
  56. GetRoles (this, role_args);
  57. if (role_args.RolesPopulated)
  58. return;
  59. }
  60. RolePrincipal principal;
  61. HttpCookie cookie = app.Request.Cookies[config.CookieName];
  62. IIdentity currentIdentity = app.Context.User.Identity;
  63. if (app.Request.IsAuthenticated) {
  64. if (cookie != null) {
  65. if (config.CacheRolesInCookie)
  66. cookie = null;
  67. else if (config.CookieRequireSSL && !app.Request.IsSecureConnection) {
  68. cookie = null;
  69. ClearCookie (app, config.CookieName);
  70. }
  71. }
  72. if (cookie == null)
  73. principal = new RolePrincipal (currentIdentity);
  74. else
  75. principal = new RolePrincipal (currentIdentity, cookie.Value);
  76. }
  77. else {
  78. /* anonymous request */
  79. if (cookie != null) {
  80. ClearCookie (app, config.CookieName);
  81. }
  82. principal = new RolePrincipal (currentIdentity);
  83. }
  84. app.Context.User = principal;
  85. Thread.CurrentPrincipal = principal;
  86. }
  87. void OnEndRequest (object sender, EventArgs args)
  88. {
  89. HttpApplication app = (HttpApplication)sender;
  90. RoleManagerSection config = (RoleManagerSection)WebConfigurationManager.GetSection ("system.web/roleManager");
  91. /* if we're not enabled or configured to cache
  92. * cookies, bail out */
  93. if (!config.Enabled || !config.CacheRolesInCookie)
  94. return;
  95. /* if the user isn't authenticated, bail
  96. * out */
  97. if (!app.Request.IsAuthenticated)
  98. return;
  99. /* if the configuration requires ssl for
  100. * cookies and we're not on an ssl connection,
  101. * bail out */
  102. if (config.CookieRequireSSL && !app.Request.IsSecureConnection)
  103. return;
  104. RolePrincipal principal = app.Context.User as RolePrincipal;
  105. if (principal == null) /* just for my sanity */
  106. return;
  107. if (!principal.CachedListChanged)
  108. return;
  109. string ticket = principal.ToEncryptedTicket ();
  110. if (ticket == null || ticket.Length > 4096) {
  111. ClearCookie (app, config.CookieName);
  112. return;
  113. }
  114. HttpCookie cookie = new HttpCookie (config.CookieName,
  115. ticket,
  116. config.CookiePath,
  117. DateTime.Now + config.CookieTimeout);
  118. cookie.Domain = config.Domain;
  119. cookie.HttpOnly = true;
  120. if (config.CookieRequireSSL)
  121. cookie.Secure = true;
  122. app.Response.SetCookie (cookie);
  123. }
  124. public void Init (HttpApplication app)
  125. {
  126. app.PostAuthenticateRequest += OnPostAuthenticateRequest;
  127. app.EndRequest += OnEndRequest;
  128. }
  129. }
  130. }
  131. #endif