.net – 何时设置HttpContext.User.Identity?

前端之家收集整理的这篇文章主要介绍了.net – 何时设置HttpContext.User.Identity?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有认证码:
var authTicket = new FormsAuthenticationTicket(/*blahblah....*/);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName,FormsAuthentication.Encrypt(authTicket));
Response.Cookies.Add(cookie);
var name = HttpContext.User.Identity.Name; // line 4

通过放入调试语句,我发现第4行的名称是空的。但是下次我在这个浏览器会话中打电话时,HttpContext.User.Identity.Name被正确设置。

那么这个值什么时候设置?

解决方法

如果给定的请求包含身份验证cookie,则将设置HttpContext.User.Identity.Name。在你的情况下,cookie刚被添加到浏览器的响应中来。浏览器会在以下请求中添加cookie(如果存在)。
原文链接:https://www.f2er.com/aspnet/253006.html

猜你在找的asp.Net相关文章