asp.net-mvc-3 - FormsAuthentication.SetAuthCookie 问题

标签 asp.net-mvc-3 cookies forms-authentication

在我的 MVC 3 应用程序中,我有一个非常基本的 Controller

[HttpPost]
public ActionResult SignIn(LogonModel logonModel)
{
    string logonMessage;
    if(_authenticationService.Login(logonModel.UserName, logonModel.Password, 
        out logonMessage))
    {
        FormsAuthentication.SetAuthCookie(logonModel.UserName,true);

        return RedirectToAction("Index", "Home");
    }

    return View();
}

我看到浏览器中设置了 cookie,但是当我关闭浏览器并返回站点时,它并没有自动让我登录。几乎就像 cookie 没有被处理,它应该被处理。

最佳答案

第一个也是最明显的问题是:您的浏览器是否启用 cookie?

您还可以检查的是,是否在您的 web.config 中,您已经配置了 <authentication>部分有超时:

<authentication mode="Forms">
    <forms loginUrl="~/login" timeout="60" />
</authentication>

如果您不指定超时,它将使用默认值 30 分钟,但也许您将其设置为其他(无效)值?

timeout属性:

Specifies the time, in integer minutes, after which the cookie expires.

您还可以检查 CookiesSupported property ( boolean ) 以查看返回结果。

关于asp.net-mvc-3 - FormsAuthentication.SetAuthCookie 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5702824/

相关文章:

asp.net - 使用 jQuery ajax 进行表单例份验证

asp.net-mvc-3 - 如何在MVC3 View 页面中修改主体类

javascript - 推荐每页只有一个文件 `.js`?

c# - "Child actions are not allowed to perform redirect actions"

asp.net-mvc - ASP.Net MVC 3 显示模板和编辑器模板自定义位置,如何?

jquery - Twitter Bootstrap Modal 和 Jquery Cookie 插件

javascript - ReactJS + Redux : How to directly navigate to a page (skip login) if token is in local storage?

javascript - 我是否应该将我的数据分布在多个 cookie 中以解决 4k 限制

c# - 如果 User.IsInRole 带有字符串数组?

asp.net - 为什么我的 ASP.NET MVC 持久身份验证 cookie 不起作用?