这是mvc4中跨域身份验证的错误吗?

时间:2013-08-20 14:01:57

标签: asp.net-mvc-4 authentication cross-domain

我在mvc3和.net4上使用跨域cookie身份验证它工作正常,很好, 我在mvc4和.net 4.5上创建另一个项目,并在mvc上将我的代码从较低版本复制到uper版本(我的意思是mvc4),现在我的验证cookie在主域上创建,但是子域无法实现用户已经过身份验证

这是mvc4中的一个错误,还是我必须启用一些这样的未来或某事?

我在主域上创建身份验证Cookie的代码:

        FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
          1,
           "fc5f06b006b44b05a257c406f4218638",//username
           DateTime.Now,
           DateTime.Now.AddDays(5),
           true,
           "members",
           FormsAuthentication.FormsCookiePath);

        // To give more security it is suggested to hash it
        string hashCookies = FormsAuthentication.Encrypt(ticket);
        HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName,
                                            hashCookies); // Hashed ticket
        cookie.Expires = DateTime.Now.AddDays(5);
        cookie.Domain = ".maindomain.com";
        Response.Cookies.Add(cookie);
子域中的

使用这行代码来测试用户身份验证:

var result = System.Web.HttpContext.Current.User.Identity.IsAuthenticated + "-" +
                 System.Web.HttpContext.Current.User.Identity.Name;

在我的全球我有:

   protected void Application_AuthenticateRequest(object sender, EventArgs e)
    {
        // look if any security information exists for this request
        if (System.Web.HttpContext.Current.User != null)
        {
            // see if this user is authenticated, any authenticated cookie (ticket) exists for this user
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                // see if the authentication is done using FormsAuthentication
                if (System.Web.HttpContext.Current.User.Identity is FormsIdentity)
                {
                    // Get the roles stored for this request from the ticket
                    // get the identity of the user
                    FormsIdentity identity = (FormsIdentity)System.Web.HttpContext.Current.User.Identity;
                    // get the forms authetication ticket of the user
                    FormsAuthenticationTicket ticket = identity.Ticket;
                    // get the roles stored as UserData into the ticket 
                    string[] roles = ticket.UserData.Split(',');
                    // create generic principal and assign it to the current request
                    System.Web.HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(identity, roles);
                }
            }
        }
    }

并在我的网络配置中:

  <authentication mode="Forms">
     <forms domain=".maindomain.com" name="atnc"  
      loginUrl="~/home" timeout="120" requireSSL="false" />
  </authentication>

0 个答案:

没有答案