Fir*_*oso 8 asp.net asp.net-mvc asp.net-mvc-4
我有一个简单的页面,我有一个登录按钮.我只是想以编程方式设置用户身份,以便当我重新导航到此页面或任何其他我可以获得当前用户身份.对此有特殊考虑吗?
gra*_*der 14
简答:
//
// Swapped FormsIdentity principal with our own custom IPrincipal
//
HttpContext.Current.User = yourPrincipalFromSomewhere;
//see
//http://www.hanselman.com/blog/SystemThreadingThreadCurrentPrincipalVsSystemWebHttpContextCurrentUserOrWhyFormsAuthenticationCanBeSubtle.aspx
//
// Sync Context Principal with Thread Principal
//
System.Threading.Thread.CurrentPrincipal = System.Web.HttpContext.Current.User;
Run Code Online (Sandbox Code Playgroud)
所以在登录时,我的团队会将IPrincipal(我们的自定义)放在这里:
HttpContext.Current.Cache.Insert
Run Code Online (Sandbox Code Playgroud)
然后在
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
}
Run Code Online (Sandbox Code Playgroud)
我们会抓住它:
HttpContext.Current.Cache
Run Code Online (Sandbox Code Playgroud)
然后重新连接到:
// Swapped FormsIdentity principal with our own IPrincipal
//
HttpContext.Current.User = cachedPrincipal;
//see
//http://www.hanselman.com/blog/SystemThreadingThreadCurrentPrincipalVsSystemWebHttpContextCurrentUserOrWhyFormsAuthenticationCanBeSubtle.aspx
//
// Sync Context Principal with Thread Principal
//
System.Threading.Thread.CurrentPrincipal = System.Web.HttpContext.Current.User;
Run Code Online (Sandbox Code Playgroud)
我是说"完全像我的团队那样做"吗?不必要.但我告诉你我们遇到的两个地方附上它.
和hanselman链接,显示有关它的一些信息.
| 归档时间: |
|
| 查看次数: |
21146 次 |
| 最近记录: |