ASP.NET子域的Cookie(父和子域) [英] ASP.NET Subdomain Cookie (parent and one subdomain)

查看:151
本文介绍了ASP.NET子域的Cookie(父和子域)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个子域,subone.parent.com,subtwo.parent.com的应用程序。

I have an app with multiple subdomains, subone.parent.com, subtwo.parent.com.

我有一个parent.com/login登录页面。当我一个用户登录基于哪一个重定向到正确的域他们中的一员。这工作得很好。

I have a logon page at parent.com/login. When a user logs in I redirect them to the proper domain based on which one they are a member of. This works fine.

FormsAuthenticationTicket ticket = new FormsAuth...
string encTicket = FormsAuthentication.Encrypt(ticket);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
cookie.Domain = subone.parent.com
Response.Cookies.Add(cookie)

这正确的验证和subone.parent.com不subtwo.parent.com用户。不过,我想做到以下几点。

This properly authenticates the user for subone.parent.com and not subtwo.parent.com. However I would like to do the following.

如果用户可以追溯到parent.com,我想知道,他们是在登录并重定向他们回到subone.parent.com。

If the user goes back to parent.com, I would like to know that they are logged in and redirect them back to subone.parent.com.

有没有实现这一点的最佳做法?还是我设置为parent.com?

Is there a best practice for accomplishing this? Or do I have to set another cookie for parent.com?

我在asp.net mvc的工作,如果它很重要。

I'm working in asp.net mvc if it matters.

谢谢!

推荐答案

您可以跨域共享饼干像你正在尝试做的,但它不是直线前进,例如这里

You can share cookies across domains like you are trying to do, but its not straight forward, example here.

另一个选项是设置cookie为.parent.com,而不是明确指定子域,并使用cookie存储子域的细节。然后你可以从任何你的子域的访问的cookie(和家长假设其www.parent.com)。

Another options is to set the cookie to be ".parent.com" rather than specifying the sub-domain explicitly and use the cookie store the details of the sub-domain. Then you can access the cookie from any of your sub-domains (and parent assuming its www.parent.com).

如果你使用MVC,你可以pretty轻松地创建自定义过滤器,并加入到www.parent.com控制器来检查cookie的存在,如果是的话重定向到子域的cookie指定。 fileters的进一步细节<一个href=\"http://www.asp.net/%28S%28pdfrohu0ajmwt445fanvj2r3%29%29/learn/mvc/tutorial-14-cs.aspx\">here.

If your using MVC, you can pretty easily create a custom filter and add to the www.parent.com controllers to check for the existence of the cookie, and if so redirect to the sub domain the cookie specifies. Further details of fileters here.

这篇关于ASP.NET子域的Cookie(父和子域)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆