来看一下MS的说明
注意:此事件在 .NET Framework 2.0 版中是新增的。
当安全模块已建立用户标识时发生。
命名空间:System.Web
程序集:System.Web(在 system.web.dll 中)
在2.0中我们使用成员组及Forms验证来实现用户的登录,但是成员组所提供的功能有时有限.我们需要能访问到自己定义的个人信息时.这时我们就能利用此事件来处理.
当然Context.User对象是实现IPrincipal, IIdentity这二个接口的.因此只要我们自定义的USER类也能实现此接口,我们就实现了一半了.
public
class MyUserObject : IPrincipal, IIdentity
{
public string Name;
public string PasswordHash;
public string PasswordSalt;
#region
IIdentity Members
public string AuthenticationType
{
get
{
return "Froms";
}
}
public bool IsAuthenticated
{
get
{
return true;
}
}
string IIdentity.Name
{
get
{
return this.Name;
}
}
#endregion
#region
IPrincipal Members
public IIdentity Identity
{
get
{
return this;
}
}
public bool IsInRole(string role)
{
return false;
}
#endregion
}
用户实体替换应该发生在HttpApplication的PostAuthenticateRequest事件发生时,因为此时ASP.NET已经从客户端得到了用户凭证Cookie并进行了解密和校验。
我们既可以编写一个HttpModule来处理PostAuthenticateRequest事件,也可以在Global.asax文件中添加事件处理器。这里为了简单,我们选择在Global.asax中添加如下事件处理器:
void
Application_PostAuthenticateRequest(object sender, EventArgs e)
{
HttpApplication application= (HttpApplication)sender;
if(application.Context.User.Identity.Name != "") //
登录成功后
{
MyUserObject user ="自己取出对象的处理";
application.Context.User = user;
}
}
下面你就可以在其他页面访问到自定义的用户实体对象了.
MyUserObject user = HttpContext.Current.User as MyUserObject