ASP.NET 中的表单身份验证问题
我使用的是 Visual Studio Team System 2008 (VSTS)、C#、.NET 3.5、IIS 7.0 和 ASP.NET。我有两个 IIS 网站,站点 A 和站点 B。它们的相关域名是 http://sitea.example.com
和 http://siteb.example.com
代码>.
我听说使用表单身份验证时,我们可以启用域级别 cookie,也就是说,如果两个站点位于同一域中(例如 sitea.example.com
和 siteb.example.com< /code> 位于域
example.com
中),最终用户只需验证一次。更具体地,如果用户通过其中一个站点的认证(通过认证),则无需在其他站点再次对用户进行认证。
如何为我的 sitea
和 siteb
启用此功能?我是否需要更改 sitea
和 siteb
的 web.config?
另外一个困惑是,如果用户通过了sitea的认证,那么可以肯定的是,sitea可以识别用户的身份,但是siteb怎么能识别呢?无需再次验证用户的身份?
I am using Visual Studio Team System 2008 (VSTS), C#, .NET 3.5, IIS 7.0, and ASP.NET. I have two IIS web sites, site A and site B. Their related domain names are, http://sitea.example.com
and http://siteb.example.com
.
I heard when using Form authentication, we could enable domain level cookies, that is, if two sites are in the same domain (e.g. both sitea.example.com
and siteb.example.com
are in domain example.com
), the end user only needs to authenticate once. In more details, if the user is authenticated (passed authentication) by one of the sites, there is no need to authenticate the user again in the other sites.
How this feature be enabled for my sitea
and siteb
? Do I need to change the web.config for both sitea
and siteb
?
Another confusion is, if the user is authenticated by sitea
, it is sure that the user's identity is recognized by sitea
, but how could siteb
recognize the user's identity without authenticating the user again?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
假设两个站点共享相同的会员数据库,那么您可以在 表单中设置 cookie 域web.config 的身份验证 部分;
请注意,您还必须在web.config,因为它们用于签署身份验证 cookie。
Assuming both sites share the same membership database then you can set the cookie domain in the forms authentication section of web.config;
Note that you'll also have to setup matching machine keys in the web.config as these are used to sign the authentication cookie.
MSDN 上有一个 示例。
There is an example on MSDN.
此链接提供了一些详细信息 http: //docs.communityserver.com/2007/270-common-things-to-check-when-using-forms-authentication/
基本上,您需要在
中添加域属性web.config 文件的
标记。
标记内的 ;例如
This link give some details http://docs.communityserver.com/2007/270-common-things-to-check-when-using-forms-authentication/
Basically you need to add the domain attribute in the
<forms/>
tag within the<authentication>
tag of the web.config file.e.g.
在 web.config 中的表单标记中将域属性设置为 .mycorp.com
Set the domain attribute to .mycorp.com in the form tag in the web.config
我建议采用 Stack Overflow、Microsoft、Facebook、Google Accounts 的方式,这甚至更有效,因为每个网站都可以位于任何不同的计算机上。
假设您有 AuthSite。这是您必须登录并包含会员信息的唯一网站。
并且您在不同的服务器上有 SiteA、SiteB 和 SiteC。
在 SiteA 的登录页面上,您必须在 AuthSite 上设置带有机密的表单帖子。
如果您之前已成功登录 AuthSite,它只会重定向回 SiteA,并以浏览器中隐藏表单帖子的形式成功提供机密,您必须在 SiteA 中进行验证。
该模型具有高度的可扩展性和可伸缩性。因为从长远来看,维护很容易。
SiteA、SiteB 和 SiteC 的登录页面代码如下。
SiteA、SiteB 和 SiteC 上的 Login.aspx:
AuthSite 上的 Login.aspx:
SiteA、SiteB 和 SiteC 上的 AuthConfirm.aspx:
现在让我们看看不同的场景。
同一用户,首次登录
同一用户,第一次访问 SiteB
页。
I would suggest the way Stack Overflow, Microsoft, Facebook, Google Accounts do, and that is even more efficient because every website can be on any different machines.
Assume, you have AuthSite. This is the one site where you have to login, and has membership information.
And you have SiteA, SiteB, and SiteC on different servers.
On login page of SiteA you have to setup a form post with a secret on AuthSite.
If you had previously logged successfully on AuthSite, it will just redirect back to SiteA with successful secret in the form of a hidden Form Post in the browser, that you have to verify in SiteA.
This model is highly extensible and scalable. Because maintanence in the long run is easy.
Code on LoginPage of SiteA, SiteB and SiteC follows.
Login.aspx on SiteA, SiteB, and SiteC:
Login.aspx on AuthSite:
AuthConfirm.aspx on SiteA, SiteB, and SiteC:
Now let's see a different scenario.
Same User, First time login
Same User, First time on SiteB
pages.