我有一个简单的页面,我有一个登录按钮。我只是想以编程方式设置User标识,以便当我重新导航到这个页面或任何其他页面时,我可以获得当前的User标识。对此有什么特别的考虑吗?
简短回答:
//
// 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;
所以在登录时,我的团队会把IPrincipal(我们自定义的)放在这里:
HttpContext.Current.Cache.Insert
然后在
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
}
我们会从:
HttpContext.Current.Cache
,然后重新连接到:
// 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;
我是在说"完全按照我的团队做"吗?不一定。但我给你们看的是我们找到的两个地方
和hanselman链接,其中显示了一些关于它的信息。
http://www.hanselman.com/blog/SystemThreadingThreadCurrentPrincipalVsSystemWebHttpContextCurrentUserOrWhyFormsAuthenticationCanBeSubtle.aspxSave userId:
Session["UserId"] = loggedInUserId;
检索标识
int loggedInUserId = (int)Session["UserId"];
你担心什么特殊的考虑?