2
0

FormsAuthenticationModule.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 reqPath = context.Request.PhysicalPath;
  41. string loginPath = context.Request.MapPath (loginPage);
  42. context.SkipAuthorization = (reqPath == loginPath);
  43. FormsAuthenticationEventArgs formArgs = new FormsAuthenticationEventArgs (context);
  44. if (Authenticate != null)
  45. Authenticate (this, formArgs);
  46. bool contextUserNull = (context.User == null);
  47. if (formArgs.User != null || !contextUserNull) {
  48. if (contextUserNull)
  49. context.User = formArgs.User;
  50. return;
  51. }
  52. HttpCookie cookie = context.Request.Cookies [cookieName];
  53. if (cookie == null || (cookie.Expires != DateTime.MinValue && cookie.Expires < DateTime.Now))
  54. return;
  55. FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt (cookie.Value);
  56. ticket = FormsAuthentication.RenewTicketIfOld (ticket);
  57. context.User = new GenericPrincipal (new FormsIdentity (ticket), new string [0]);
  58. cookie.Value = FormsAuthentication.Encrypt (ticket);
  59. cookie.Path = cookiePath;
  60. if (ticket.IsPersistent)
  61. cookie.Expires = ticket.Expiration;
  62. context.Response.Cookies.Add (cookie);
  63. }
  64. void OnEndRequest (object sender, EventArgs args)
  65. {
  66. if (noForms)
  67. return;
  68. HttpApplication app = (HttpApplication) sender;
  69. HttpContext context = app.Context;
  70. if (context.Response.StatusCode != 401 || context.Request.QueryString ["ReturnUrl"] != null)
  71. return;
  72. AuthConfig config = (AuthConfig) context.GetConfig ("system.web/authentication");
  73. StringBuilder login = new StringBuilder ();
  74. login.Append (UrlUtils.Combine (context.Request.ApplicationPath, config.LoginUrl));
  75. login.AppendFormat ("?ReturnUrl={0}", HttpUtility.UrlEncode (context.Request.RawUrl));
  76. context.Response.Redirect (login.ToString ());
  77. }
  78. public event FormsAuthenticationEventHandler Authenticate;
  79. }
  80. }