我有验证码:
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
通过放入调试语句,我发现
name
第 4 行是空的。但是下次我在这个浏览器 session 上打电话时,HttpContext.User.Identity.Name
设置正确。那么这个值什么时候设置呢?
最佳答案
如果给定的请求包含身份验证 cookie,则将设置 HttpContext.User.Identity.Name。在您的情况下,cookie 刚刚添加到浏览器的响应中以获取。如果存在,浏览器将在以下请求中添加 cookie。
关于.net - 何时设置 HttpContext.User.Identity?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6594734/