【问题标题】:Session and Form Authentication Problem : Asp.Net MVC 3会话和表单身份验证问题:Asp.Net MVC 3
【发布时间】:2011-01-30 17:05:08
【问题描述】:

当我在开发 Asp.Net MVC 3 应用程序时,我在我的应用程序中使用了FormAuthentication

问题是,在登录系统后,当我关闭浏览器(没有注销)并再次在浏览器中打开页面(比如说/Admin/ProductList/)时,page is still being invokedI got focus in my controller too。 [这真的很糟糕! :( ]

我想要的是,当我关闭浏览器并再次返回任何页面时,它应该转到logged in page

查看给定的代码以便您理解。

public void SignIn(string userName, bool isCookiePersistent)
        {

            FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, userName, DateTime.Now, DateTime.Now.AddDays(14),
                createPersistentCookie, string.Empty);

            HttpCookie authCookie = FormsAuthentication.GetAuthCookie(userName, isCookiePersistent);
            if (authTicket.IsPersistent)
            {
                authCookie.Expires = authTicket.Expiration;
            }

            authCookie.Value = FormsAuthentication.Encrypt(authTicket);
            HttpContext.Current.Response.Cookies.Add(authCookie);
        }

public void SignOut()
        {
            FormsAuthentication.SignOut();
        }

Web.Config 代码:

<authentication mode="Forms">
      <forms loginUrl="~/Admin/Login" timeout="2880" />
    </authentication>

My page is getting in **Redirection Loop**: This is the main issue.

我错过了any other settings or global.asax event handling吗?

请给我任何解决方案来帮助我。

提前致谢。

【问题讨论】:

    标签: asp.net asp.net-mvc session asp.net-mvc-3


    【解决方案1】:

    这里:

    authCookie.Expires = authTicket.Expiration;
    

    这就是使身份验证 cookie 持久存在的原因,并且浏览器将其存储在客户端计算机上,因此当您重新启动浏览器时,cookie 仍然存在。如果你不想要持久性 cookie,你可以试试这个:

    public void SignIn(string userName)
    {
        var authTicket = new FormsAuthenticationTicket(
            1, userName, DateTime.Now, DateTime.Now.AddDays(14), false, string.Empty
        );
        var authCookie = FormsAuthentication.GetAuthCookie(userName, false);
        authCookie.Value = FormsAuthentication.Encrypt(authTicket);
        HttpContext.Current.Response.Cookies.Add(authCookie);
    }
    

    【讨论】:

    • 感谢您的回复!但这不起作用,实际上当我关闭浏览器并再次返回任何网址时,页面显示为空白!什么都没有发生……它以空白页的形式打开……
    • @nunu,您尝试导航到的操作是否用[Authorize] 属性修饰?如果用户未通过身份验证,这将导致重定向到登录屏幕。
    • 不,我没有使用 [Authorize] 属性。实际上,当我回到任何页面时,它都会以空白页面打开,无论我请求哪个页面......
    • @nunu,您应该使用[Authorize] 属性来装饰您希望对其进行身份验证的所有控制器操作。为了避免装饰所有这些,您只能装饰控制器。就空白页而言,您的浏览器上可能会留下一些 cookie。在重试之前尝试清除所有 cookie。
    猜你喜欢
    • 2011-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-16
    相关资源
    最近更新 更多