FormsAuthenticationModule.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. bool noForms;
  20. public void Dispose ()
  21. {
  22. }
  23. public void Init (HttpApplication app)
  24. {
  25. app.AuthenticateRequest += new EventHandler (OnAuthenticateRequest);
  26. app.EndRequest += new EventHandler (OnEndRequest);
  27. }
  28. void OnAuthenticateRequest (object sender, EventArgs args)
  29. {
  30. HttpApplication app = (HttpApplication) sender;
  31. HttpContext context = app.Context;
  32. AuthConfig config = (AuthConfig) context.GetConfig ("system.web/authentication");
  33. if (config.Mode != AuthenticationMode.Forms) {
  34. noForms = true;
  35. return;
  36. }
  37. string cookieName = config.CookieName;
  38. string cookiePath = config.CookiePath;
  39. string loginPage = config.LoginUrl;
  40. string appVPath = context.Request.ApplicationPath;
  41. string reqPath = context.Request.Path;
  42. if (reqPath.StartsWith (appVPath))
  43. reqPath = reqPath.Substring (appVPath.Length);
  44. context.SkipAuthorization = (reqPath == loginPage);
  45. FormsAuthenticationEventArgs formArgs = new FormsAuthenticationEventArgs (context);
  46. if (Authenticate != null)
  47. Authenticate (this, formArgs);
  48. bool contextUserNull = (context.User == null);
  49. if (formArgs.User != null || !contextUserNull) {
  50. if (contextUserNull)
  51. context.User = formArgs.User;
  52. return;
  53. }
  54. HttpCookie cookie = context.Request.Cookies [cookieName];
  55. if (cookie == null || (cookie.Expires != DateTime.MinValue && cookie.Expires < DateTime.Now))
  56. return;
  57. FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt (cookie.Value);
  58. ticket = FormsAuthentication.RenewTicketIfOld (ticket);
  59. context.User = new GenericPrincipal (new FormsIdentity (ticket), new string [0]);
  60. cookie.Value = FormsAuthentication.Encrypt (ticket);
  61. cookie.Path = cookiePath;
  62. if (ticket.IsPersistent)
  63. cookie.Expires = ticket.Expiration;
  64. context.Response.Cookies.Add (cookie);
  65. }
  66. void OnEndRequest (object sender, EventArgs args)
  67. {
  68. if (noForms)
  69. return;
  70. HttpApplication app = (HttpApplication) sender;
  71. HttpContext context = app.Context;
  72. if (context.Response.StatusCode != 401 || context.Request.QueryString ["ReturnUrl"] != null)
  73. return;
  74. AuthConfig config = (AuthConfig) context.GetConfig ("system.web/authentication");
  75. StringBuilder login = new StringBuilder ();
  76. login.Append (UrlUtils.Combine (context.Request.ApplicationPath, config.LoginUrl));
  77. login.AppendFormat ("?ReturnUrl={0}", HttpUtility.UrlEncode (context.Request.RawUrl));
  78. context.Response.Redirect (login.ToString ());
  79. }
  80. public event FormsAuthenticationEventHandler Authenticate;
  81. }
  82. }