朋友们有的知道Wrox.ThePhile中的权限许可的设计吧,
我按照这个设计思想,自己也做了一个,可出现了以下的错误指定的转换无效。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.InvalidCastException: 指定的转换无效。源错误:
WenyiPrincipal currUser = (WenyiPrincipal)Context.User;//就是这里出错
Response.Write(currUser.MyIdentity.EmailAddress);而我的WenyiPrincipal是从System.Security.Principal.IPrincipal派生的
对Context.User的赋值在用户登陆后:
WenyiPrincipal newUser=new WenyiPrincipal(name);
Context.User = newUser;
我按照这个设计思想,自己也做了一个,可出现了以下的错误指定的转换无效。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.InvalidCastException: 指定的转换无效。源错误:
WenyiPrincipal currUser = (WenyiPrincipal)Context.User;//就是这里出错
Response.Write(currUser.MyIdentity.EmailAddress);而我的WenyiPrincipal是从System.Security.Principal.IPrincipal派生的
对Context.User的赋值在用户登陆后:
WenyiPrincipal newUser=new WenyiPrincipal(name);
Context.User = newUser;
Context.User==null
吗?
Context.User==null
吗?
参考
http://www.microsoft.com/china/technet/security/guidance/secmod38.mspx
http://www.codeproject.com/aspnet/formsroleauth.asp
登陆时我是对Context.User赋值为一个WenyiPrincipal类型的对象啊
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
。。
CustomPrincipal newUser = new CustomPrincipal(identity, roles);
Context.User = newUser;
}
' we() haven't replaced the default context user with our own.
' Let's do that now. We know that the previous context.user.
' identity.name is the e-mail address (because we forced it to be
' as such in the login.aspx page) Dim newUser As New SitePrincipal(Context.User.Identity.Name)
Context.User = newUser跟上面的老大们说的在global.asax改是一个原理。因为phile定义了个基类所以不需要在global.asax改了。不过我自己的问题来了,我要siteprincipal 传递两个变量怎么办阿?只有一个name属性....
Application_AuthenticateRequest
在一次将你自定义的Principal类
赋值给他
这样你就可以在任何页面上都可以用你
自己的自定义类了
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion注意这里是先调用InitializeComponent()后调用父类的base.OnInit(e),如果这样的话就在页面载入是先调用的是自己的Page_Load()方法,后调用父类的OnInit(e),这样PhilePage的Phile_Load()就形同虚设了。只要把它们的顺序换过来就OK了。