3

这是一个非常烦人的问题,我不知道从哪里开始寻找解决它。

当我在本地运行代码时不会发生此问题,仅当我在实时环境中运行它时才会发生。我知道我遗漏了一些东西,这可能很明显我可以弄清楚。

好的,我有一个 MVC 4 应用程序,我在其中覆盖了 MembershipProvider。我很确定这不是问题,因为登录确实有效。登录在 IE、Firefox、Safari 上有效,但在 Chrome 桌面 (Windows 7) 和 Chrome IOS(在 Ipad 上)上失败。

在 Chrome 上,用户需要登录两次。在第一次尝试时,未设置 cookie 的 ASPXAUTH 元素。但它是在第二次尝试时设置的。(在提琴手审查)

我环顾四周,发现了这个建议,我已经实施了它,但我仍然有问题。(这并没有完全详细说明我的问题,但很接近)

http://www.hanselman.com/blog/FormsAuthenticationOnASPNETSitesWithTheGoogleChromeBrowserOnIOS.aspx

(我安装了 4.5,所以我知道这是一个很长的镜头)

我不知道从哪里开始寻找解决这个问题。这是我创建cookie的代码。

    private void SignIn(string userName, bool RememberMe)
    {
        MyMembershipProvider provider = (MyMembershipProvider)System.Web.Security.Membership.Provider;
        MyMembershipUser user = (MyMembershipUser)provider.GetUser(userName, false);

        FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
          user.UserName,
          DateTime.Now,
          DateTime.Now.AddMinutes(30),
          true,
          user.UserID.ToString(),
          FormsAuthentication.FormsCookiePath);

        // Encrypt the ticket.
        string encTicket = FormsAuthentication.Encrypt(ticket);

        // Create the cookie.
        HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);

        Response.Cookies.Add(authCookie);
    }

创建cookie后,我重定向

    return Redirect("Https://www.sitehome.local");

请注意,我在 HTTPS 下运行整个站点,以防导致问题

4

1 回答 1

1

正如我们在评论中发现的那样,cookie 没有在重定向时被转发,而是通过以这种方式创建 cookie 来修复:

var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, ticketString) 
    { 
       Secure = FormsAuthentication.RequireSSL, 
       Path = FormsAuthentication.FormsCookiePath, 
       HttpOnly = true, 
       Domain = FormsAuthentication.CookieDomain 
    };

Response.Cookies.Add(authCookie);
于 2013-01-18T14:20:40.957 回答