gpt4 book ai didi

asp.net-core - Asp.Net Core 2.0 和 Azure AD B2C 用于 WebApp 和 API 上的身份验证

转载 作者:行者123 更新时间:2023-12-02 04:55:21 25 4
gpt4 key购买 nike

我有一个用于测试的现有小型应用程序,它的 Web 应用程序和 API 均采用 Asp.Net Core 1.1,身份验证是使用 Azure AD B2C 完成的。我正在尝试将其移至 .Net Core 2.0,但我不知道如何让它工作,我尝试使用 GitHub Azure-Samples 中的示例用于 Web 应用程序和 API,但尝试时出现未经授权或 500 错误要访问 api,如果您有一个使用 2.0 从 Web 应用程序调用 Web api 并受 AD B2C 保护的工作示例,我们将不胜感激。

编辑:我用来测试的示例是:网络应用程序:WebApp-OpenIDConnect-DotNet core2.0网络API:B2C-WebApi core2.0,我更改了 appsettings 值以匹配我的 b2c 目录。

对于我的 asp.net core 1.1 测试应用程序,我使用与上面相同的示例,但来自 master 分支,并且 appsettings 具有相同的值。

编辑2默认情况下,在startup.cs中我有这个:

        services.AddAuthentication()
.AddJwtBearer(option => new JwtBearerOptions
{
Authority = string.Format("https://login.microsoftonline.com/tfp/{0}/{1}/v2.0/",
Configuration["Authentication:AzureAd:Tenant"], Configuration["Authentication:AzureAd:Policy"]),
Audience = Configuration["Authentication:AzureAd:ClientId"],
Events = new JwtBearerEvents
{
OnAuthenticationFailed = AuthenticationFailed
}
});

这给了我以下错误:

Microsoft.AspNetCore.Hosting.Internal.WebHost:信息:请求开始 HTTP/1.1 GET http://localhost:44352/api/values/5
Microsoft.AspNetCore.Server.Kestrel:错误:连接 ID“0HL89JHF4VBLM”,请求 ID“0HL89JHF4VBLM:00000001”:应用程序引发了未处理的异常。System.InvalidOperationException:未指定authenticationScheme,并且未找到DefaultChallengeScheme。

如果修改了 services.AddAuthentication 这样的

        services.AddAuthentication(sharedOption =>
{
sharedOption.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})

现在错误

Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler:信息: 无法验证 token xxx。Microsoft.IdentityModel.Tokens.SecurityTokenInvalidSignatureException:IDX10500:签名验证失败。没有提供安全 key 来验证签名。 在System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateSignature(字符串 token ,TokenValidationParameters验证参数) 在System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateToken(字符串 token ,TokenValidationParameters验证参数,SecurityToken&validatedToken) 在 Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler.d__6.MoveNext()

最佳答案

我在示例上看到了一个拉取请求,该请求纠正了此问题( Link ),services.AddAuthentication 必须更改为:

        services.AddAuthentication(options =>
{
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(jwtOptions =>
{
jwtOptions.Authority = $"https://login.microsoftonline.com/tfp/{Configuration["Authentication:AzureAd:Tenant"]}/{Configuration["Authentication:AzureAd:Policy"]}/v2.0/";
jwtOptions.Audience = Configuration["Authentication:AzureAd:ClientId"];
jwtOptions.Events = new JwtBearerEvents
{
OnAuthenticationFailed = AuthenticationFailed
};
});

关于asp.net-core - Asp.Net Core 2.0 和 Azure AD B2C 用于 WebApp 和 API 上的身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46515272/

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