c# - 在网站之间共享 cookie

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

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

https://learn.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();
    }

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

 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

最佳答案

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

您的网址不同!

您可以使用 virtual directory在 IIS 或 Sub Domain .

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

关于c# - 在网站之间共享 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68288360/

相关文章:

c# - 是否可以在我的应用程序中添加数据源连接向导?

c# - 如何将resx资源文件变成字典

iOS 11 密码自动填充不会将密码保存到钥匙串(keychain)

authentication - Grails Micro Service外部化身份验证

c# - 如何将 Display 属性添加到外部枚举?

c# - 删除 DateTime.UctNow.TimeOfDay 的毫秒部分的最简单方法是什么?

c# - 无法在类库应用程序中找到 security.dll 库

c# - 检索进程网络使用情况

.net - 最好在任务内部还是外部抛出 NotSupportedException?

node.js - 在express.js中维护用户登录状态