asp.net-core - 在 JwtBearerOptions 中指定用于 Azure Active Directory 身份验证的客户端 key

标签 asp.net-core jwt azure-active-directory openid-connect

如果我在 Visual Studio 2017 中创建新的 ASP.NET Core MVC 应用程序,我可以使用一行将客户端 key 添加到 AzureAdServiceCollectionExtensions:

public void Configure(string name, OpenIdConnectOptions options)
{
    options.ClientId = _azureOptions.ClientId;
    // Add this line below
    options.ClientSecret = _azureOptions.ClientSecret;
    options.Authority = $"{_azureOptions.Instance}{_azureOptions.TenantId}";
    options.UseTokenLifetime = true;
    options.CallbackPath = _azureOptions.CallbackPath;
    options.RequireHttpsMetadata = false;
}

配置 POCO 已连接以从配置文件中获取它。

另一方面,在 Web API 项目中,身份验证使用 JWT token ,并且 Visual Studio 生成的代码有所不同:

public void Configure(string name, JwtBearerOptions options)
{
    options.Audience = _azureOptions.ClientId;
    options.Authority = $"{_azureOptions.Instance}{_azureOptions.TenantId}";
}

我不确定在 JwtBearOptions 中的哪个位置放置客户端 key 。

最佳答案

您实际上并未在选项中指定客户端 key 。当您使用对称 key 签署 token 时会使用它。

public void Configure(string name, JwtBearerOptions options)
{
    options.Audience = _azureOptions.ClientId;
    options.Authority = $"{_azureOptions.Instance}{_azureOptions.TenantId}";
    var secretKey = "mysupersecret_secretkey!123";
    var signingKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(secretKey));
    options.TokenValidationParameters.IssuerSigningKey = signingKey;
}

查看这些链接:

关于asp.net-core - 在 JwtBearerOptions 中指定用于 Azure Active Directory 身份验证的客户端 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46897492/

相关文章:

azure-active-directory - 通过传递 Azure B2C JWT Bearer token 从 Angular 调用 .NET CORE Web API 安全端点

c# - 在 Asp.net Core Web API 中返回 "JsonResult/IActionResult"或 "Task<SomeObject>"或仅返回 "SomeObject"有什么区别?

c# - WireMock.Net 如何响应有时错误和其他 OK

c# - .net core razor 页面中的多个 View 组件未正确绑定(bind)

python - JWT 从断言解码 - Google 登录

asp.net-mvc - 将参数传递给 Azure Active Directory 身份验证

ssl - 在 AWS 中调用 IdentityServer4 时获取 "IDX10108: The address specified is not valid as per HTTPS scheme"

javascript - 如何从 Node api 中的获取请求中解码 JWT token

php - 没有数据库的 Laravel 中的 JWT 身份验证

azure - 在 AAD 应用程序注册中显示与托管服务身份服务主体关联的应用程序