跨子域的表单身份验证

Miy*_*der 26 c# asp.net forms-authentication

当身份验证发生在子域而不是父域时,是否可以跨子域验证用户?

例如:

用户登录到site1.parent.com,然后我们需要将它们发送到reporting.parent.com.

即使在子域中发生登录,我是否可以对报告站点进行身份验证?

到目前为止,我所做的所有研究都让用户首先登录到父域,然后每个子域都可以访问身份验证cookie.

jro*_*jro 29

对用户进行身份验证时,请将身份验证cookie的域设置为二级域,即parent.com.每个子域将根据请求接收父域的cookie,因此可以对每个子域进行身份验证,因为您将使用共享身份验证cookie.

验证码:

System.Web.HttpCookie authcookie = System.Web.Security.FormsAuthentication.GetAuthCookie(UserName, False);
authcookie.Domain = "parent.com";
HttpResponse.AppendCookie(authcookie);
HttpResponse.Redirect(System.Web.Security.FormsAuthentication.GetRedirectUrl(UserName, 
                                                                       False));
Run Code Online (Sandbox Code Playgroud)


Jef*_*tin 10

您可以在身份验证时将cookie设置为父域,但您必须明确设置它,它将默认为您所在的完整域.

将auth cookie正确设置为父域后,所有子域都应该能够读取它.


Tr1*_*tan 7

作为旁注,我发现在使用jro的方法运行良好+1后,FormsAuthenication.SignOut()方法在从www /以外的子域调用时不起作用.(我猜是因为.Domain属性不匹配) - 为了解决这个问题,我使用了:

if (Request.Cookies[FormsAuthentication.FormsCookieName] != null)
            {
                HttpCookie myCookie = new HttpCookie(FormsAuthentication.FormsCookieName);
                myCookie.Domain = "parent.com";
                myCookie.Expires = DateTime.Now.AddDays(-1d);
                Response.Cookies.Add(myCookie);
            }
Run Code Online (Sandbox Code Playgroud)


Jac*_*0ws 7

除了将cookie设置为父域之外,还需要确保所有站点(apps)具有相同的validationKey和decryptionKey(),以便它们都能识别彼此的身份验证票证和cookie.相当不错的文章http://www.codeproject.com/KB/aspnet/SingleSignon.aspx


Dha*_*777 5

Jro的回答工作正常。但是一定要更新 webconfig 表单身份验证setting "domain" ,否则表单身份验证注销将无法正常工作。是我遇到的注销问题。这里的技巧是有一个“。” 作为域的前缀为 cookie 设置为“.parent.com”(使用 cookie 检查器)。

<authentication mode="Forms">          
      <forms cookieless="UseCookies" defaultUrl="~/Default" loginUrl="~/user/signin" domain=".parent.com"  name="FormAuthentication" path="/"/>
    </authentication>
Run Code Online (Sandbox Code Playgroud)