Asp.net 为其他站点存储cookie

Asp.net 为其他站点存储cookie,asp.net,cookies,Asp.net,Cookies,我有多个asp.net网站。当用户登录到其中一个站点时,我想存储一个cookie,告诉我用户已登录。当用户稍后访问我拥有的其他站点时,我希望从该站点读取cookie 如果你既不能从其他站点读取cookie,也不能向其他站点写入cookie,那么解决方法是什么呢? 也许要重定向到 给我一些想法:-)您正在寻找解决方案。如果您可以在同一域下的不同子域中托管您的站点,您可以保存整个域共享的cookie,例如: “site1.yourdomain.com”和 “site2.yourdomain.com”

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

如果你既不能从其他站点读取cookie,也不能向其他站点写入cookie,那么解决方法是什么呢? 也许要重定向到


给我一些想法:-)

您正在寻找解决方案。

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

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

两者都可以读取保存到域“yourdomain.com”的cookie吗


另一种方法是通过请求将登录信息告知其他站点,如重定向建议。您可以通过多种方式实现这一点,例如,通过在iframe中加载页面、将数据直接从一台服务器发送到另一台服务器等等。但是,这些都不是特别优雅的,在登录的情况下,正如Tomas Lycken所说,您应该真正使用正确的SSO实现。

我们的一个客户有完全相同的要求(登录到不同域上的多个站点),其中一个站点要求用户登录到一个经典的ASP应用程序、.NET 1.1应用程序和.NET 3.5应用程序,这些应用程序运行在不同的硬件上,但运行在同一个域中,这一事实使情况变得复杂

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

因此(更改页面和域以保护无辜者):

  • 用户请求www.example1.com/page1.aspx
  • 设置了一个cookie,告诉我们用户试图访问page1.aspx,并将用户发送到www.example1.com/login.aspx
  • 用户登录,然后重定向到www.example2.com/processlogin.aspx?token=EncryptedToken
  • ProcessLogin.aspx检查一个cookie,告诉它将用户指向何处,如果找不到cookie,它将解密令牌,让用户登录example2.com,然后将其重定向到www.example1.com/ProcessLogin.aspx?token=EncryptedToken(或example3.com-根据需要重复)
  • 与第4部分一样,ProcessLogin.aspx检查cookie,找到它,删除它,并将用户重定向到/page1.aspx
  • 如果用户稍后在身份验证票证超时之前访问www.example2.com上的页面,他们也将登录该站点


    编辑以回复评论

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

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

    您可以在IFrame中生成对另一个页面的请求,或者尝试在自动关闭的弹出窗口中执行该操作-但这会产生弹出窗口阻止程序、窗口闪烁等其他问题

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

    非常基本的代码设置:

    一个.aspx页面,包含一个控件,该控件的LoggedIn事件附加了一个方法“OnLoggedIn”:

    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);
    }
    

    不,我不这么认为,因为你只能登录其中一个网站。我想从其他站点读取的cookie只包含用户在某个时间登录到“主”站点的信息。这听起来像我所想的。你似乎在重定向,我在考虑我的情况,我能不能直接向其他页面发出请求?由于我是ASP.net的新手,您能告诉我这段代码是什么样子的吗?