【问题标题】:How to validate JWT Bearer Token in .NET Core 2.2如何在 .NET Core 2.2 中验证 JWT 不记名令牌
【发布时间】:2020-01-20 17:07:36
【问题描述】:

我开发了一个前端应用程序,它设法从 Azure AD 获取 jwt 令牌。

我设法将此令牌放在标头中并将请求发送到我的后端。

我用在线工具检查了令牌,它是有效的。

在不需要令牌的 URL 上,我可以获取它并对其进行解码。

但是当我注意到一个 URL 为 [Authorize] 时,我仍然得到答案 401,而且我不知道该怎么做才能让我的后端验证我的令牌。

我不知道在我的 .NET Core 2.2 项目的 Startup.cs 中添加什么

有人有解决办法吗?

【问题讨论】:

标签: azure .net-core jwt token


【解决方案1】:

我找到了解决办法:

在 appsettings.json 中:

"AzureActiveDirectory": {
    "Instance": "https://login.microsoftonline.com/",
    "Domain": "<Domain>",
    "TenantId": "https://login.microsoftonline.com/<TenantId>/v2.0",
    "ClientId": "<ClientId>"
  }

在 Startup.cs 中:

services.AddAuthentication(AzureADDefaults.BearerAuthenticationScheme).AddAzureADBearer(options => Configuration.Bind("AzureActiveDirectory", options));
services.AddAuthorization(o =>
{
    o.DefaultPolicy = new AuthorizationPolicyBuilder()
        .RequireAuthenticatedUser()
        .AddAuthenticationSchemes(AzureADDefaults.BearerAuthenticationScheme)
        .Build();
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-27
    • 2017-10-26
    • 2019-12-01
    • 2020-02-24
    • 1970-01-01
    • 2020-08-22
    • 1970-01-01
    • 2016-09-25
    相关资源
    最近更新 更多