.Net Core 5 AddMicrosoftIdentityWebAppAuthentication,现在如何结合OpenId和Bearer?
.Net Core 5 AddMicrosoftIdentityWebAppAuthentication, how to combine OpenId and Bearer now?
从 .NET Core 3 移植到 .NET Core 5 身份验证,我需要用 services.AddMicrosoftIdentityWebAppAuthentication 或其他相关的 MicrosoftIdentity 扩展替换 services.AddSignIn(已弃用)。我需要同时支持 OpenId 用户登录和 Bearer 令牌策略以进行 API 访问。这些都与 services.AddSignIn 一起工作,但我无法让这个组合与新的 MicrosftIdentity 扩展一起工作。
我有一个 .Net Core Blazor 服务器,使用 OpenId 登录到 Azure AD B2C。这很好用:
services.AddMicrosoftIdentityWebAppAuthentication(
configuration,
aadB2CConfigName,
OpenIdConnectDefaults.AuthenticationScheme,
CookieAuthenticationDefaults.AuthenticationScheme,
true);
但是当我直接在它后面添加 Bearer Token 处理时,如下所示,它会覆盖 OpenId,因此只有 Bearer API 有效(反之亦然):
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApi(
options =>
{
configuration.Bind("AzureAd", options);
options.ForwardDefaultSelector = ctx =>
ctx.Request.Path.StartsWithSegments("/api", System.StringComparison.InvariantCulture)
? JwtBearerDefaults.AuthenticationScheme
: null;
options.Authority = configuration["AzureAd:Authority"];
options.TokenValidationParameters.NameClaimType = "name";
options.TokenValidationParameters.RoleClaimType = "roles";
}, options =>
{
configuration.Bind(aadB2CConfigName, options);
});
为了完整性,后面是(在 .net core 3 中运行良好)
services.AddControllersWithViews()
.AddMicrosoftIdentityUI();
services.AddAuthorization(options =>
{
options.AddPolicy("Tst1AuthPolicy", policy => policy.RequireRole("Tst1AppRole"));
options.AddPolicy("Tst2AuthPolicy", policy => policy.RequireRole("Tst2AppRole"));
});
扩展方法的链接似乎不像 AddSignIn 那样有效。 如何在新的 .net core 5 上下文中使这两项再次并行工作?即默认为 OpenId,当 WebApi (/api) 被命中时为 Bearer Tokens。
得到它的工作如下。我不完全相信这是 intended/best 的用法,但它有效:
将上面的“null”替换为备用登录方案,在我们的例子中是 oreo cookie 方案:
options.ForwardDefaultSelector = ctx =>
ctx.Request.Path.StartsWithSegments("/api", System.StringComparison.InvariantCulture)
? JwtBearerDefaults.AuthenticationScheme
: CookieAuthenticationDefaults.AuthenticationScheme;
这可能意味着,如果 Bearer Token 失败,我会得到一个 HTML 登录页面,而不是 HTTP 状态错误,所以必须有更好的方法......如果有人愿意提示英寸
从 .NET Core 3 移植到 .NET Core 5 身份验证,我需要用 services.AddMicrosoftIdentityWebAppAuthentication 或其他相关的 MicrosoftIdentity 扩展替换 services.AddSignIn(已弃用)。我需要同时支持 OpenId 用户登录和 Bearer 令牌策略以进行 API 访问。这些都与 services.AddSignIn 一起工作,但我无法让这个组合与新的 MicrosftIdentity 扩展一起工作。
我有一个 .Net Core Blazor 服务器,使用 OpenId 登录到 Azure AD B2C。这很好用:
services.AddMicrosoftIdentityWebAppAuthentication(
configuration,
aadB2CConfigName,
OpenIdConnectDefaults.AuthenticationScheme,
CookieAuthenticationDefaults.AuthenticationScheme,
true);
但是当我直接在它后面添加 Bearer Token 处理时,如下所示,它会覆盖 OpenId,因此只有 Bearer API 有效(反之亦然):
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApi(
options =>
{
configuration.Bind("AzureAd", options);
options.ForwardDefaultSelector = ctx =>
ctx.Request.Path.StartsWithSegments("/api", System.StringComparison.InvariantCulture)
? JwtBearerDefaults.AuthenticationScheme
: null;
options.Authority = configuration["AzureAd:Authority"];
options.TokenValidationParameters.NameClaimType = "name";
options.TokenValidationParameters.RoleClaimType = "roles";
}, options =>
{
configuration.Bind(aadB2CConfigName, options);
});
为了完整性,后面是(在 .net core 3 中运行良好)
services.AddControllersWithViews()
.AddMicrosoftIdentityUI();
services.AddAuthorization(options =>
{
options.AddPolicy("Tst1AuthPolicy", policy => policy.RequireRole("Tst1AppRole"));
options.AddPolicy("Tst2AuthPolicy", policy => policy.RequireRole("Tst2AppRole"));
});
扩展方法的链接似乎不像 AddSignIn 那样有效。 如何在新的 .net core 5 上下文中使这两项再次并行工作?即默认为 OpenId,当 WebApi (/api) 被命中时为 Bearer Tokens。
得到它的工作如下。我不完全相信这是 intended/best 的用法,但它有效:
将上面的“null”替换为备用登录方案,在我们的例子中是 oreo cookie 方案:
options.ForwardDefaultSelector = ctx =>
ctx.Request.Path.StartsWithSegments("/api", System.StringComparison.InvariantCulture)
? JwtBearerDefaults.AuthenticationScheme
: CookieAuthenticationDefaults.AuthenticationScheme;
这可能意味着,如果 Bearer Token 失败,我会得到一个 HTML 登录页面,而不是 HTTP 状态错误,所以必须有更好的方法......如果有人愿意提示英寸