跨子域的表单身份验证

发布于 2024-07-14 02:09:34 字数 219 浏览 6 评论 0原文

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

例如:

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

即使登录是在子域中进行的,我是否可以向报告站点验证它们的身份?

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

Is it possible to authenticate users across sub-domains when the authentication takes place at a sub-domain instead of the parent domain?

For example:

User logs into site1.parent.com, and then we need to send them to reporting.parent.com.

Can I authenticate them to the reporting site even though the log-in occured at a sub-domain?

So far all of the research I have done has users logging into the parent domain first and then each sub-domain has access to the authentication cookie.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

翻身的咸鱼 2024-07-21 02:09:34

当您对用户进行身份验证时,将身份验证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));

When you authenticate the user, set the authentication cookie's domain to the second-level domain, i.e. parent.com. Each sub-domain will receive the parent domain's cookies on request, so authentication over each is possible since you will have a shared authentication cookie to work with.

Authentication code:

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));
决绝 2024-07-21 02:09:34

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

一旦将身份验证 cookie 正确设置到父域,那么所有子域都应该能够读取它。

You can set the cookie to be the parent domain at authentication time but you have to explicitly set it, it will default to the full domain that you are on.

Once the auth cookie is correctly set to the parent domain, then all sub-domains should be able to read it.

无尽的现实 2024-07-21 02:09:34

作为旁注,我发现在使用 jro 的方法(效果很好+1)之后,当从 www/ 以外的子域调用时,FormsAuthenication.SignOut() 方法不起作用。 (我猜测是因为 .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);
            }

As a side note, I found that after using jro's method which worked well +1, the FormsAuthenication.SignOut() method didn't work when called from a subdomain other than www/. (I'm guessing because the .Domain property doesn't match) - To get around this I used:

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);
            }
千里故人稀 2024-07-21 02:09:34

除了为父域设置cookie之外,还需要确保所有站点(应用程序)具有相同的validationKey和decryptionKey(),以便它们都能识别彼此的身份验证票证和cookie。 这里的文章非常好 http://www.codeproject.com/KB/aspnet/SingleSignon.aspx

In addition to setting a cookie to parent domain also need to make sure that all sites (apps) have same validationKey and decryptionKey () so they all recognise each other's authentication ticket and cookie. Pretty good article here http://www.codeproject.com/KB/aspnet/SingleSignon.aspx

安人多梦 2024-07-21 02:09:34

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

<authentication mode="Forms">          
      <forms cookieless="UseCookies" defaultUrl="~/Default" loginUrl="~/user/signin" domain=".parent.com"  name="FormAuthentication" path="/"/>
    </authentication>

Jro's answer works fine. But make sure to update the webconfig forms authentication setting "domain"
, otherwise forms authentication signout will not work properly. Here is the signout issue I came across. Trick here is to have a '.' as the prefix as the domain is set for the cookie as ".parent.com" (use a cookie inspector).

<authentication mode="Forms">          
      <forms cookieless="UseCookies" defaultUrl="~/Default" loginUrl="~/user/signin" domain=".parent.com"  name="FormAuthentication" path="/"/>
    </authentication>
审判长 2024-07-21 02:09:34

是的,当然。 您可能需要在某些阶段自行推出,但这应该是可行的。

一个想法:当您将它们重定向到边界时,给它们一个一次性传递令牌,然后告诉接收子域期待它们(此用户,来自此 IP,带有此令牌)。

Yes, sure. You may need to roll your own at some stages, but it should be doable.

One idea: as you redirect them across the boundary, give them a one-time pass token and then tell the receiving sub-domain to expect them (this user, from this IP, with this token).

假面具 2024-07-21 02:09:34

需要做的 2 件事:

  1. 所有 web.config(主域和子域)中的 MachineKey 应该相同
  2. AuthenticationCookie 域名应该相同。

请遵循以下文章更深入。

2 things to be done:

  1. MachineKey should be same in all web.config (main domain and sub domain(s))
  2. AuthenticationCookie domain name should be same.

Follow the following article for more depth.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文