存储其他网站的cookie

时间:2009-09-02 13:02:11

标签: asp.net cookies

我有多个asp.net网站。当用户登录其中一个站点时,我想存储一个cookie,告诉我用户已登录。当用户稍后访问我拥有的其他站点时,我想从该站点读取cookie。

AFAIK你既不能从其他网站读取cookie或将cookie写入其他网站,那么解决方法是什么呢? 也许重定向到http://www.othersite.com/SaveCookie.aspx

给我一​​些想法: - )

3 个答案:

答案 0 :(得分:3)

我们的一个客户具有完全相同的要求(登录到不同域上的多个站点),其中一个站点要求用户登录到经典ASP应用程序,.NET 1.1应用程序和运行在不同硬件上但在同一域下的.NET 3.5应用程序...

我们基本上实现了循环式重定向系统,其中每个域都会记录用户,然后将它们弹回到下一个域,直到它们返回到原始域,此时它们将重定向到其原始请求

所以(改变页面和域名以保护无辜者):

  1. 用户请求 www.example1.com/page1.aspx
  2. 设置了一个Cookie,告诉我们用户正在尝试访问page1.aspx,并且用户被发送到 www.example1.com/login.aspx
  3. 用户登录,然后重定向到 www.example2.com/processlogin.aspx?token=EncryptedToken
  4. ProcessLogin.aspx检查cookie,告诉它在哪里引导用户,如果找不到,则解密令牌,在example2.com上记录用户,然后将它们重定向到 www。 example1.com/processlogin.aspx?token=EncryptedToken (或example3.com - 根据需要重复)
  5. 与4中一样,ProcessLogin.aspx检查cookie,找到它,删除它并将用户重定向到/page1.aspx。
  6. 如果用户稍后访问www.example2.com上的页面,则在身份验证票证超时之前,他们仍将在该站点上登录。


    编辑以回复评论

    这取决于你如何向其他页面发出“请求”。如果您从后面的代码发出请求,那么您正在做的是在服务器上而不是在用户浏览器上有效地设置cookie。

    Cookie需要由服务器发布到客户端浏览器,并且在页面响应的标题中完成 - 因此您需要将用户浏览器定向到另一个站点上的页面以从该域发出cookie

    您可以在IFrame中为其他页面生成请求,或者在自动关闭弹出窗口中尝试执行此操作 - 但是还有其他问题,如弹出窗口阻止程序,闪烁窗口等。

    经过一番调查,我们发现像这样的循环重定向是最简单,最可靠的解决方案。

    一个非常基本的代码设置:

    一个包含Login控件的.aspx页面,其中一个方法“OnLoggedIn”附加到控件的LoggedIn事件中:

    void OnLoggedIn(object sender, EventArgs e){
      string returnUrl = Request.QueryString["returnUrl"];
    
      // Create new cookie, store value, and add to cookie collection
      HttpCookie myCookie = new HttpCookie("WhereTo");
      myCookie["ReturnUrl"] = ReturnUrl;
      Response.Cookies.Add(myCookie);
    
      // Redirect user to roundtrip login processor on next domain.
      // Work out domain as required.
      string redirect = GetNextDomain();
      // Add encoded user token
      redirect += "?token=" + EncodeUserToken();
    
      // Redirect the user, and end further processing on this thread
      Response.Redirect(redirect, true);
    }
    

    然后在两台服务器上都有ProcessLogin.aspx,其中包含类似的内容:

    protected void Page_Load(object sender, EventArgs e){
      // Look for redirect cookie
      if (Request.Cookies["WhereTo"]["ReturnUrl"] != null){
        // Save value from cookie
        string redirect = Request.Cookies["WhereTo"]["ReturnUrl"];
    
        // Delete original cookie by creating an empty one, and setting it
        // to expire yesterday, and add it to the response.
        HttpCookie myCookie = new HttpCookie("WhereTo");
        myCookie.Expires = DateTime.Now.AddDays(-1d);
        Response.Cookies.Add(myCookie);
    
        // Redirect the user, and stop processing
        Response.Redirect(redirect, true);
      }
    
      // Still here, so log in and redirect
      string encryptedToken = Request.QueryString["token"];
    
      if (!string.IsNullOrEmpty(encryptedToken)){
        // Decrypt token, and log user in
        // This will vary depending on your authentication mechanism
        PerformLogin(encryptedToken);
      }
    
      // Redirect user to roundtrip login processor on next domain.
      // Work out domain as required.
      string redirect = GetNextDomain();
      // Add encoded user token - no need to recalculate, it will be the same
      redirect += "?token=" + encryptedToken;
    
      // Redirect the user, and end further processing on this thread
      Response.Redirect(redirect, true);
    }
    

答案 1 :(得分:2)

您正在寻找Single Sign-On (SSO)解决方案。

答案 2 :(得分:0)

如果您可以在同一域下的不同子域中托管您的网站,则可以保存为整个域共享的Cookie,例如:

“site1.yourdomain.com”和 “site2.yourdomain.com”

可以读取保存到域“yourdomain.com”

的cookie

另一种方法是通过请求向其他网站告知登录,如重定向建议。您可以通过多种方式完成此操作,例如:通过在iframe中加载页面,将数据直接从一个服务器发送到另一个服务器,依此类推。但是,这些都不是特别优雅,并且在登录的情况下,正如Tomas Lycken所说,你应该真正开始实施适当的SSO。

相关问题