gpt4 book ai didi

asp.net-core - ASP.NET Core 3.1 同时使用 OpenIDConnect 和自定义 Cookie 身份验证

转载 作者:行者123 更新时间:2023-12-05 02:45:34 27 4
gpt4 key购买 nike

我有一个使用 Cookie 身份验证 的现有应用程序,我想添加使用 Active Directory 对用户进行身份验证的功能。当前应用程序使用基于 Cookie 的 authentication 和自定义 authorisation - 数据库中的角色。

我正在从位于此处的示例中添加位:

Add sign-in with Microsoft to an ASP.NET Core web app

当我运行应用程序时出现错误:

System.InvalidOperationException: Scheme already exists: Cookies

配置 OpenIdConnectCookie 身份验证 的正确方法是什么。

//第 1 步基本 Cookie 验证

services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = "/Auth";
options.AccessDeniedPath = "/Home/AccessDenied";
options.Cookie.IsEssential = true;
options.SlidingExpiration = true;
options.ExpireTimeSpan = TimeSpan.FromSeconds(day/2.0);
options.Cookie.HttpOnly = true; // not accessible via JavaScript
options.Cookie.Name = "login_token";

options.TicketDataFormat = new CustomJwtDataFormat(
SecurityAlgorithms.HmacSha256,
tokenValidationParameters);
});

//第 2 步 OpenID Connect 身份验证

services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(Configuration.GetSection("AzureAd"), "OpenIdConnect", "Cookies", true);

我找不到同时使用 Cookie 身份验证OpenID Connect 的任何示例。这可能吗? 允许用户使用 Active Directory 身份验证或本地身份验证(详细信息存储在本地数据库中)有选择地登录。

更改“Cookie”名称后,去掉错误信息,但破坏了本地授权,例如

当给出有效的用户名和密码时,我通常授权登录。

HttpContext.Response.Cookies.Append("login_token", token, GetCookieOptions());

当前配置了 OpenIDConnect User.Identity.IsAuthenticated仍然是错误的。

最佳答案

根据错误信息,它告诉你你有多个名为cookies的Scheme。

根据 AddMicrosoftIdentityWebApp 方法 document ,你会发现第三个参数名称是cookieScheme。

要使用的基于 cookie 的方案名称。默认情况下,它使用“Cookies”。

但是你已经在上面设置了这个名字,所以你应该使用其他的。例如:“ADCookies”。

如下所示:

services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(Configuration.GetSection("AzureAd"), "OpenIdConnect", "ADCookies", true);

关于asp.net-core - ASP.NET Core 3.1 同时使用 OpenIDConnect 和自定义 Cookie 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65930262/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com