在网站之间共享 cookie

时间:2021-07-07 14:54:19

标签: c# .net authentication cookies model-view-controller

我指的是在下面的教程中在本地运行的 2 个不同的 MVC 应用程序之间共享 cookie,

https://docs.microsoft.com/en-us/aspnet/core/security/cookie-sharing?view=aspnetcore-5.0

BaseApp2 : 在 https://localhost:44363/ 上运行的配置如下

 public void ConfigureServices(IServiceCollection services)
    {
        DirectoryInfo di = new DirectoryInfo(@"C:\SharedCookies");
        services.AddDataProtection()
        .PersistKeysToFileSystem(di)
        .SetApplicationName("SharedCookieApp");

        services.ConfigureApplicationCookie(options =>
        {
            options.Cookie.Name = ".AspNet.SharedCookie";
        });

        services.AddAuthentication(options =>
        {
            options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        })
          .AddCookie(options =>
          {
              options.LoginPath = new PathString("/Account/SignIn");
          })
          .AddOktaMvc(new OktaMvcOptions
          {
              // Replace these values with your Okta configuration
              OktaDomain = Configuration.GetValue<string>("Okta:OktaDomain"),
              ClientId = Configuration.GetValue<string>("Okta:ClientId"),
              ClientSecret = Configuration.GetValue<string>("Okta:ClientSecret"),
              AuthorizationServerId = Configuration.GetValue<string>("Okta:AuthorizationServerId"),
              Scope = new List<string> { "openid", "profile", "email" },

          });
        services.AddControllersWithViews();
    }

应该重用在 https://localhost:44309/ 上运行的 baseapp2 cookie 的 Subapp1 具有以下配置,

 public void ConfigureServices(IServiceCollection services)
    {
        DirectoryInfo di = new DirectoryInfo(@"C:\SharedCookies");
        services.AddDataProtection()
        .PersistKeysToFileSystem(di)
        .SetApplicationName("SharedCookieApp");

        services.ConfigureApplicationCookie(options =>
        {
            options.Cookie.Name = ".AspNet.SharedCookie";
            options.Cookie.Path = @"C:\SharedCookies";// "/";
        });

        services.AddControllersWithViews();
    }

当我成功登录到 baseapp2 时,我可以看到 cookie 正在它的域中创建。并且它也被保存到那里提到的物理路径。但是我无法使用该 cookie 登录到第二个应用程序?

有什么遗漏吗?请帮忙。

附上截图

enter image description here

enter image description here

4 个答案:

答案 0 :(得分:1)

您的 cookie 路径值很奇怪:

<块引用>

Path 属性指定了此 Cookie 适用的源服务器上的 URI 子集。如果未指定此属性,则此 Cookie 将发送到源服务器或服务器上的所有页面。

  • 可能你会想要 / 用于两个网站。
  • 可能还指定了相同的 cookie 域(在开发中不需要,可能在生产中,这取决于应用的部署方式)。

答案 1 :(得分:1)

两个不同的域(例如 mydomain.com 和 subdomain.mydomain.com,或 sub1.mydomain.com 和 sub2.mydomain.com)只有在 Set-Cookie 标头中明确命名域时才能共享 cookie。否则,cookie 的范围仅限于请求主机。 (这称为“仅限主机的 cookie”。请参阅 What is a host only cookie?

您的网址不同!

您可以在 IIS 或 {​​{3}} 中使用 virtual directory

所有现代浏览器都遵守较新的规范 Sub Domain,并且会忽略任何前导点,这意味着您可以在子域和顶级域上使用 cookie。

答案 2 :(得分:0)

移除 options.Cookie.Path = @"C:\SharedCookies";// "/";来自 subapp1 的行 要么 将其添加到基本应用配置中。

我做过类似的事情,我不需要明确指定 cookie 路径。

答案 3 :(得分:0)

你可以试试这个方法:

创建一个文件,在所有 3 个域上设置 cookie。然后创建一个 HTML 文件,该文件将加载在其他 2 个域上设置 cookie 的文件。示例:

<html>
   <head></head>
   <body>
      <p>Please wait.....</p>
      <img src="http://domain2.com/setcookie?theme=1" />
      <img src="http://domain3.com/setcookie?theme=2" />
   </body>
</html>

然后在 body 标签上添加一个 onload 回调。仅当图像完全加载时才会加载文档,即在其他 2 个域上设置 cookie 时。

如果您可以在 C# 中创建此功能,那么您就可以在网站之间共享 cookie

相关问题