【发布时间】:2016-01-12 22:37:19
【问题描述】:
这个 cookie 被设置时叫什么?我可以在浏览器的 cookie 文件夹中看到它吗?
我认为它没有设置,所以我的登录验证失败。
【问题讨论】:
标签: c# asp.net authentication forms-authentication
这个 cookie 被设置时叫什么?我可以在浏览器的 cookie 文件夹中看到它吗?
我认为它没有设置,所以我的登录验证失败。
【问题讨论】:
标签: c# asp.net authentication forms-authentication
这个 cookie 被设置时叫什么?
cookie 的默认名称是.ASPXAUTH。 configurable 通过 web.config 可以有任何名称。
<authentication mode="Forms">
<forms name=".SomeName" loginUrl="Login.aspx" />
</authentication>
所以检查那里是否有任何不标准的地方。
我可以在浏览器的 cookie 文件夹中看到它吗?
您应该能够使用任何浏览器工具看到它。
【讨论】:
通常,如果您自己创建身份验证Cookie,则需要创建 Principal 对象并将其保存在 AuthenticateRequest 事件中的 Current Thread 中每个请求。
否则,当你检查User.Identity.IsAuthenticated时,它会返回false。
public class Global : HttpApplication
{
private void Application_AuthenticateRequest(object sender, EventArgs e)
{
HttpCookie decryptedCookie =
Context.Request.Cookies[FormsAuthentication.FormsCookieName];
FormsAuthenticationTicket ticket =
FormsAuthentication.Decrypt(decryptedCookie.Value);
var identity = new GenericIdentity(ticket.Name);
var principal = new GenericPrincipal(identity, null);
HttpContext.Current.User = principal;
Thread.CurrentPrincipal = HttpContext.Current.User;
}
}
确保您在 web.config 中有身份验证标签。例如,
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880"/>
</authentication>
public ActionResult Index()
{
var username = User.Identity.Name;
return View();
}
我使用EditThisCookie Chrome 插件。
【讨论】: