Gor*_*ran 5 asp.net authentication identity principal
在ASP.net应用程序中,我正在使用我编写的自定义成员资格提供程序的Login控件.我想要做的是Thread.CurrentPrincipal
在用户通过身份验证后设置为我的自定义Principal对象.
我正在使用setter:Thread.CurrentPrincipal
它为我设置了Principal对象,但是在所有后续线程上,这个CurrentPrincipal被默认值覆盖.
这是我的Login控件的Authenticate事件的代码:
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
string username = Login1.UserName;
string password = Login1.Password;
if (Membership.ValidateUser(username, password))
{
var login = sender as Login;
var phoenixIdentity = new PhoenixIdentity("B", "Forms" , true);
var principal = new PhoenixPrincipal(phoenixIdentity);
Thread.CurrentPrincipal = principal;
AppDomain.CurrentDomain.SetThreadPrincipal(principal);
HttpContext.Current.User = principal;
e.Authenticated = true;
}
}
Run Code Online (Sandbox Code Playgroud)
例如,假设我使用用户名A登录,一切顺利......验证通过,但我使用Identity对象中的用户名B对用户进行硬编码,该对象设置为我设置为CurrentPrincipal
对象的Principal 对象.
当我CurrentPrincipal
在此方法结束时检查哪个用户设置为Identity时,它表示它是用户B.但是当我加载另一个页面然后检查其身份CurrentPrincipal
是什么时,它表示它是用户A.
那么,如何让我的CurrentPrincipal
对象在所有其他线程中保持持久性,以及此Login控件何时/何时设置CurrentPrincipal
Thread 的对象?
您可以处理 FormsAuthentication_OnAuthenticate(object sender, FormsAuthenticationEventArgs e) (在 Global.asax 中)并在此处设置 CurrentPrincipal。
void FormsAuthentication_OnAuthenticate(object sender, FormsAuthenticationEventArgs e)
{
var phoenixIdentity = new PhoenixIdentity("B", "Forms" , true);
var principal = new PhoenixPrincipal(phoenixIdentity);
e.User = principal;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
11943 次 |
最近记录: |