Files
mono/mcs/class/System.Web/System.Web.Security/FormsAuthenticationModule.cs
T
Gonzalo Paniagua Javier 8e35e3a7b9 2002-12-20 Gonzalo Paniagua Javier <[email protected]>
* System.Web/HttpCookie.cs: send 'expires' in the header.
	* System.Web.Security/FormsAuthenticationModule.cs: remove debug lines.
	* System.Web.UI/BaseParser.cs: a couple of path fixes to make it work
	when the page is not in the root directory.
	* System.Web.Util/UrlUtils.cs: fixed IsRelativeUrl and IsRootUrl.

svn path=/trunk/mcs/; revision=9797
2002-12-20 13:49:48 +00:00

93 lines
2.5 KiB
C#

//
// System.Web.Security.FormsAuthenticationModule
//
// Authors:
// Gonzalo Paniagua Javier ([email protected])
//
// (C) 2002 Ximian, Inc (http://www.ximian.com)
//
using System;
using System.Security.Principal;
using System.Web;
using System.Web.Configuration;
namespace System.Web.Security
{
public sealed class FormsAuthenticationModule : IHttpModule
{
bool noForms;
public void Dispose ()
{
}
public void Init (HttpApplication app)
{
app.AuthenticateRequest += new EventHandler (OnAuthenticateRequest);
app.EndRequest += new EventHandler (OnEndRequest);
}
void OnAuthenticateRequest (object sender, EventArgs args)
{
HttpApplication app = (HttpApplication) sender;
HttpContext context = app.Context;
AuthConfig config = (AuthConfig) context.GetConfig ("system.web/authentication");
if (config.Mode != AuthenticationMode.Forms) {
noForms = true;
return;
}
string cookieName = config.CookieName;
string cookiePath = config.CookiePath;
string loginPage = config.LoginUrl;
FormsAuthenticationEventArgs formArgs = new FormsAuthenticationEventArgs (context);
if (Authenticate != null)
Authenticate (this, formArgs);
bool contextUserNull = (context.User == null);
if (formArgs.User != null || !contextUserNull) {
if (contextUserNull)
context.User = formArgs.User;
return;
}
HttpCookie cookie = context.Request.Cookies [cookieName];
if (cookie == null || (cookie.Expires != DateTime.MinValue && cookie.Expires < DateTime.Now))
return;
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt (cookie.Value);
FormsAuthentication.RenewTicketIfOld (ticket);
context.User = new GenericPrincipal (new FormsIdentity (ticket), new string [0]);
Console.WriteLine ("name: " + ticket.Name);
cookie.Value = FormsAuthentication.Encrypt (ticket);
cookie.Path = cookiePath;
if (ticket.IsPersistent)
cookie.Expires = ticket.Expiration;
context.Response.Cookies.Add (cookie);
context.SkipAuthorization = (context.Request.Path == loginPage);
}
void OnEndRequest (object sender, EventArgs args)
{
if (noForms)
return;
HttpApplication app = (HttpApplication) sender;
HttpContext context = app.Context;
if (context.Response.StatusCode != 401)
return;
AuthConfig config = (AuthConfig) context.GetConfig ("system.web/authentication");
Console.WriteLine ("Redirecting to login page: '{0}'", config.LoginUrl);
context.Response.Redirect (config.LoginUrl);
}
public event FormsAuthenticationEventHandler Authenticate;
}
}