PostAuthenticateRequest 我们能做什么
2008-01-24 10:53
316 查看
来看一下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
注意:此事件在 .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
相关文章推荐
- PostAuthenticateRequest 我们能做什么
- PHP中$_POST,$_GET,$_REQUEST,$_FILES全局变量的全局指什么
- XMLHttpRequest.open();第一个参数post,get有什么不同,什么时候选什么,还有其他的,都有什么区别?
- 我们能从java的HelloWorld学到什么?
- 我们能从围棋中学到什么? 推荐
- C#使用HttpWebRequest POST或GET信息
- 表单提交方式中的get和post有什么区别?
- 模型驱动开发能帮我们解决什么
- Get和Post的用法,Request.QuerySt…
- ajax XMLHttpRequest post get
- 扩展方法为我们带来了什么
- 当“退信”成为攻击,我们能够做什么?
- 教育的本质是什么?——“我们能从大学得到什么”系列之二 推荐
- 新劳动法给我们带来了什么?
- Django POST CSRF verification failed. Request aborted
- 加入.NET圈子带来的思考,什么是RSS?RSS能带给我们什么?
- 神经网络模型无法正常工作时我们应该做什么(系列)——Batch Size
- HttpWebRequest POST 数据时请求头多了一行Expect: 100-continue,少了数据行
- 我们的资源和优势是什么——苍蝇采蜜的原因
- JQuery中Ajax的Post提交中文乱码、windows.location.href 中文汉字乱码和springmvc使用原生态HttpServletRequest接收参数中文乱码解决办法