2
0

FormsAuthenticationModule.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // System.Web.Security.FormsAuthenticationModule
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2002 Ximian, Inc (http://www.ximian.com)
  8. //
  9. using System;
  10. using System.Security.Principal;
  11. using System.Text;
  12. using System.Web;
  13. using System.Web.Configuration;
  14. using System.Web.Util;
  15. namespace System.Web.Security
  16. {
  17. public sealed class FormsAuthenticationModule : IHttpModule
  18. {
  19. public void Dispose ()
  20. {
  21. }
  22. public void Init (HttpApplication app)
  23. {
  24. app.AuthenticateRequest += new EventHandler (OnAuthenticateRequest);
  25. app.EndRequest += new EventHandler (OnEndRequest);
  26. }
  27. void OnAuthenticateRequest (object sender, EventArgs args)
  28. {
  29. HttpApplication app = (HttpApplication) sender;
  30. HttpContext context = app.Context;
  31. AuthConfig config = (AuthConfig) context.GetConfig ("system.web/authentication");
  32. if (config.Mode != AuthenticationMode.Forms) {
  33. return;
  34. }
  35. string cookieName = config.CookieName;
  36. string cookiePath = config.CookiePath;
  37. string loginPage = config.LoginUrl;
  38. string reqPath = context.Request.PhysicalPath;
  39. string loginPath = context.Request.MapPath (loginPage);
  40. context.SkipAuthorization = (reqPath == loginPath);
  41. FormsAuthenticationEventArgs formArgs = new FormsAuthenticationEventArgs (context);
  42. if (Authenticate != null)
  43. Authenticate (this, formArgs);
  44. bool contextUserNull = (context.User == null);
  45. if (formArgs.User != null || !contextUserNull) {
  46. if (contextUserNull)
  47. context.User = formArgs.User;
  48. return;
  49. }
  50. HttpCookie cookie = context.Request.Cookies [cookieName];
  51. if (cookie == null || (cookie.Expires != DateTime.MinValue && cookie.Expires < DateTime.Now))
  52. return;
  53. FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt (cookie.Value);
  54. ticket = FormsAuthentication.RenewTicketIfOld (ticket);
  55. context.User = new GenericPrincipal (new FormsIdentity (ticket), new string [0]);
  56. cookie.Value = FormsAuthentication.Encrypt (ticket);
  57. cookie.Path = cookiePath;
  58. if (ticket.IsPersistent)
  59. cookie.Expires = ticket.Expiration;
  60. context.Response.Cookies.Add (cookie);
  61. }
  62. void OnEndRequest (object sender, EventArgs args)
  63. {
  64. HttpApplication app = (HttpApplication) sender;
  65. HttpContext context = app.Context;
  66. if (context.Response.StatusCode != 401 || context.Request.QueryString ["ReturnUrl"] != null)
  67. return;
  68. AuthConfig config = (AuthConfig) context.GetConfig ("system.web/authentication");
  69. if (config.Mode != AuthenticationMode.Forms)
  70. return;
  71. StringBuilder login = new StringBuilder ();
  72. login.Append (UrlUtils.Combine (context.Request.ApplicationPath, config.LoginUrl));
  73. login.AppendFormat ("?ReturnUrl={0}", HttpUtility.UrlEncode (context.Request.RawUrl));
  74. context.Response.Redirect (login.ToString ());
  75. }
  76. public event FormsAuthenticationEventHandler Authenticate;
  77. }
  78. }