ASP.NET 中的表单身份验证问题

发布于 2024-08-04 20:24:37 字数 628 浏览 3 评论 0原文

我使用的是 Visual Studio Team System 2008 (VSTS)、C#、.NET 3.5、IIS 7.0 和 ASP.NET。我有两个 IIS 网站,站点 A 和站点 B。它们的相关域名是 http://sitea.example.comhttp://siteb.example.com代码>.

我听说使用表单身份验证时,我们可以启用域级别 cookie,也就是说,如果两个站点位于同一域中(例如 sitea.example.comsiteb.example.com< /code> 位于域 example.com 中),最终用户只需验证一次。更具体地,如果用户通过其中一个站点的认证(通过认证),则无需在其他站点再次对用户进行认证。

如何为我的 siteasiteb 启用此功能?我是否需要更改 siteasiteb 的 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 技术交流群。

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

发布评论

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

评论(5

梦魇绽荼蘼 2024-08-11 20:24:37

假设两个站点共享相同的会员数据库,那么您可以在 表单中设置 cookie 域web.config 的身份验证 部分;

<authentication mode="Forms">
    <forms .... domain="mycorp.com"/>
</authentication>

请注意,您还必须在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;

<authentication mode="Forms">
    <forms .... domain="mycorp.com"/>
</authentication>

Note that you'll also have to setup matching machine keys in the web.config as these are used to sign the authentication cookie.

撞了怀 2024-08-11 20:24:37

MSDN 上有一个 示例

There is an example on MSDN.

套路撩心 2024-08-11 20:24:37

此链接提供了一些详细信息 http: //docs.communityserver.com/2007/270-common-things-to-check-when-using-forms-authentication/

基本上,您需要在 中添加域属性web.config 文件的 标记内的 ; 标记。

例如

<authentication mode="Forms">
<forms name=".CookieName" ... domain=".mydomain.com" />
</authentication>

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.

<authentication mode="Forms">
<forms name=".CookieName" ... domain=".mydomain.com" />
</authentication>
清风挽心 2024-08-11 20:24:37

Set the domain attribute to .mycorp.com in the form tag in the web.config

百合的盛世恋 2024-08-11 20:24:37

我建议采用 Stack Overflow、Microsoft、Facebook、Google Accounts 的方式,这甚至更有效,因为每个网站都可以位于任何不同的计算机上。

假设您有 AuthSite。这是您必须登录并包含会员信息的唯一网站。

并且您在不同的服务器上有 SiteA、SiteB 和 SiteC。

在 SiteA 的登录页面上,您必须在 AuthSite 上设置带有机密的表单帖子。

如果您之前已成功登录 AuthSite,它只会重定向回 SiteA,并以浏览器中隐藏表单帖子的形式成功提供机密,您必须在 SiteA 中进行验证。

该模型具有高度的可扩展性和可伸缩性。因为从长远来看,维护很容易。

SiteA、SiteB 和 SiteC 的登录页面代码如下。

SiteA、SiteB 和 SiteC 上的 Login.aspx

private void Page_Load(object sender, EventArg e){
   // Simply redirect back to AuthSite...
   // Change Site parameter accordingly.
   Response.Redirect("http://authsite/Login.aspx?Site=SiteA");
}

AuthSite 上的 Login.aspx

// Define one hidden field named "ReturnSite".

private void Page_Load(object sender, EventArg e){

   if(IsPostBack)
       return;
   string site = Request.QueryString["Site"];
   if(Request.User.IsAuthenticated){
       string secrete = CreateSomeSecrete(site);
       Response.Redirect("http://" + site + 
           "/AuthConfirm.aspx?Token=" + secrete + 
           "&User=" + Request.User.Identity.Name);
       return;
   }

   ReturnSite.value = site;
   // Do usual login...
}

private void LoginButton_Click(object sender, EventArg e){
   string secrete = CreateSomeSecrete(ReturnSite.value);
   FormAuthentication.SetAuthCookie(username,true);
   // You can retrive username later by calling 
   // Request.User.Identity.Name.
   Response.Redirect("http://" + ReturnSite.value + 
      "/AuthConfirm.aspx?Token=" + secrete + "&User=" + username);
}

SiteA、SiteB 和 SiteC 上的 AuthConfirm.aspx

private void Page_Load(object sender, EventArg e){
   string secrete = Request.QueryString["Token"];
   // Verify that secret came only from AuthSite.
   if(VerifySecrete(secrete)){
       // This sets authentication cookie for Current Site
       FormsAuthentication.SetAuthCookie(Request.QueryString["User"], true);
   }
}

现在让我们看看不同的场景。

同一用户,首次登录

  1. 第一个用户 John 访问 SiteA(尚未登录)时会被重定向到 AuthSite。
  2. AuthSite 检查并发现用户没有身份验证 cookie,因此会询问实际凭据。
  3. AuthSite 在自身上设置令牌并将机密传递到 SiteA 上的 AuthConfirm 页面。 SiteA 验证令牌并设置身份验证 cookie,并允许用户访问安全页面。

同一用户,第一次访问 SiteB

  1. 用户 John 已使用 AuthSite 成功登录到 SiteA,现在尝试访问 SiteB。
  2. SiteB 发现用户未登录,因此将其定向到 AuthSite。
  3. AuthSite 发现用户已经拥有 AuthSite 网站的 cookie。
  4. AuthSite 使用身份验证密钥将用户重定向回 SiteB。
  5. SiteB 验证秘密并让 John 继续安全访问
    页。

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:

private void Page_Load(object sender, EventArg e){
   // Simply redirect back to AuthSite...
   // Change Site parameter accordingly.
   Response.Redirect("http://authsite/Login.aspx?Site=SiteA");
}

Login.aspx on AuthSite:

// Define one hidden field named "ReturnSite".

private void Page_Load(object sender, EventArg e){

   if(IsPostBack)
       return;
   string site = Request.QueryString["Site"];
   if(Request.User.IsAuthenticated){
       string secrete = CreateSomeSecrete(site);
       Response.Redirect("http://" + site + 
           "/AuthConfirm.aspx?Token=" + secrete + 
           "&User=" + Request.User.Identity.Name);
       return;
   }

   ReturnSite.value = site;
   // Do usual login...
}

private void LoginButton_Click(object sender, EventArg e){
   string secrete = CreateSomeSecrete(ReturnSite.value);
   FormAuthentication.SetAuthCookie(username,true);
   // You can retrive username later by calling 
   // Request.User.Identity.Name.
   Response.Redirect("http://" + ReturnSite.value + 
      "/AuthConfirm.aspx?Token=" + secrete + "&User=" + username);
}

AuthConfirm.aspx on SiteA, SiteB, and SiteC:

private void Page_Load(object sender, EventArg e){
   string secrete = Request.QueryString["Token"];
   // Verify that secret came only from AuthSite.
   if(VerifySecrete(secrete)){
       // This sets authentication cookie for Current Site
       FormsAuthentication.SetAuthCookie(Request.QueryString["User"], true);
   }
}

Now let's see a different scenario.

Same User, First time login

  1. The first user, John, visiting SiteA (not yet logged in) gets redirected to AuthSite.
  2. AuthSite checks and finds out that user does not have an authentication cookie, so actual credentials are asked.
  3. AuthSite sets token on itself and passes secret to AuthConfirm page on SiteA. SiteA verifies the token and sets the authentication cookie and lets user to visit secure pages.

Same User, First time on SiteB

  1. User John is successfully logged into SiteA using AuthSite, now tries to visit SiteB.
  2. SiteB finds the user is not logged in so it is directed to AuthSite.
  3. AuthSite finds that the user already has a cookie for AuthSite website.
  4. AuthSite redirects user back to SiteB with the authentication secret.
  5. SiteB verifies the secret and lets John continue to visit secure
    pages.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文