【问题标题】:asp.net core custom bearer token and Azure Ad token authorizationasp.net core 自定义不记名令牌和 Azure Ad 令牌授权
【发布时间】:2021-10-05 02:08:12
【问题描述】:

我会尽力解释我的问题。

目前我正在处理用户可以使用本地 db 用户登录并可以使用 Azure AD 登录的要求,使用本地 db 用户我正在使用以下代码创建自定义令牌。

public static string GenerateJwtToken(string userId, string userName, string secret)
    {
        var tokenHandler = new JwtSecurityTokenHandler();
        var key = Encoding.ASCII.GetBytes(secret);

        var tokenDescriptor = new SecurityTokenDescriptor
        {
            Subject = new ClaimsIdentity(new[]
            {
                new Claim(ClaimTypes.NameIdentifier, userId),
                new Claim(ClaimTypes.Name, userName)
            }),
            Expires = DateTime.UtcNow.AddDays(7),
            SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
        };
        var token = tokenHandler.CreateToken(tokenDescriptor);
        var encryptedToken = tokenHandler.WriteToken(token);

        return encryptedToken;
    }

并使用以下代码验证令牌。

        public static IServiceCollection AddJwtAuthentication(this IServiceCollection services, IdentityAppSettings appSettings)
    {
        var key = Encoding.ASCII.GetBytes(appSettings.SecretKey);

        services
            .AddAuthentication(x =>
            {
                x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(x =>
            {
                x.RequireHttpsMetadata = false;
                x.SaveToken = true;
                x.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = false,
                    IssuerSigningKey = new SymmetricSecurityKey(key),
                    ValidateIssuer = false,
                    ValidateAudience = false
                };
            });
        return services;
    }

对于 Azure AD,我在 statup.cs 类中使用以下代码

services.AddMicrosoftIdentityWebApiAuthentication(Configuration, "AzureAd");

appsettings.json

  "AzureAd": {
"Instance": "https://login.microsoftonline.com/",
"TenantId": "11121313213213132132",
"ClientId": "46853216534163",
"Audience": "api://11121313213213132132"

},

但它抛出错误“方案已存在:Bearer”

现在我的问题是我们如何一起使用多个令牌验证器,以及我们如何从一个验证器中检查令牌是否有效,因此我们不应该签入另一个验证器。

【问题讨论】:

标签: .net asp.net-core azure-active-directory jwt access-token


【解决方案1】:

谢谢Chaodeng。发布您的建议作为帮助其他社区成员的答案。

需要从 AddAuthentication 中移除服务

使用 JWT 默认方案 JwtBearerDefaults.AuthenticationScheme 的 Azure AD 身份验证

这是示例配置

 services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer("MyAppName",options =>
            {
                 options.Authority = "Value";
                 options.Audience = "Value";                    
            })
            .AddMicrosoftIdentityWebApi(Configuration, "AzureAd");

这里是SO了解更多信息

【讨论】:

    猜你喜欢
    • 2020-07-09
    • 2018-06-05
    • 1970-01-01
    • 1970-01-01
    • 2017-10-26
    • 2021-02-21
    • 2021-08-02
    • 2021-01-01
    • 2020-07-25
    相关资源
    最近更新 更多