我指的是在本地运行的 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 登录到第二个应用程序?
有什么遗漏吗?请帮忙。
附上截图
最佳答案
两个不同的域(例如 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/