【发布时间】:2022-11-08 00:56:57
【问题描述】:
如果用户在我的应用程序中登录,我正在尝试使用第二个 cookie,以便子域中的其他应用程序可以验证。但是我不信任其他应用程序,这就是我打算使用两个身份验证 Cookie 的原因。一个用于我自己的应用程序(IdentityCookie),一个用于其他应用程序可以访问登录状态(SubCookie)。
我正在使用 ASP.NET Identity 进行 cookie 创建和帐户管理。
services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>().AddSignInManager<AuthSignInManager<ApplicationUser>>();
services.ConfigureApplicationCookie(options =>
{
options.Cookie.SameSite = Microsoft.AspNetCore.Http.SameSiteMode.Strict;
options.CookieManager = new CookieManager();
});
自定义的 CookieManager 负责同时创建和删除两个 Cookie。
我现在的问题是我找不到验证 cookie 的方法。我尝试使用自定义 Authenticationhandler 来验证 subCookie,但如果我添加以下代码,IdentityCookie 验证将停止工作:
services.AddAuthentication("CookieBearer").AddScheme<BasicAuthenticationOptions, BasicAuthenticationHandler>("CookieBearer", o => {});
添加这一行后,似乎只使用了自定义处理程序,而忽略了 Identity 提供的处理程序。有没有办法用 ASP.NET Identity 添加多个 AuthenticationHandlers?我想将自定义 AuthenticationHandler 用作后备选项。因此,如果身份验证失败,则使用自定义 AuthenticationHandler。
我知道我可以像这样链接身份验证方案/方法,但我不确定如何与 Identity 结合使用。
services
.AddAuthentication()
.AddJwtBearer("Custom1", options =>
{
//Configure here
})
.AddJwtBearer("Custom2", options =>
{
// Configure here
});
【问题讨论】:
标签: c# asp.net authentication cookies