我有一个项目,其中包括一个Chrome扩展作为前端和ASP.NET Core 7 Web API作为后端.该项目正在对OAuth使用Azure Entra ID.

使用该扩展,我能够成功检索承载令牌并将其传递给API.API似乎完全验证了令牌,但是来自控制器的所有响应都是401状态.

以下是与身份验证/授权相关的Program.cs代码:

builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddMicrosoftIdentityWebApi(builder.Configuration.GetSection("AzureAd"));
    app.UseHttpsRedirection();
    app.UseCors();
    app.UseAuthentication();
    app.UseAuthorization();
    app.MapControllers();
    app.Run();

下面是我设置的一个基本控制器,用于try 和测试身份验证:

[Authorize]
[ApiController]
[Route("controller")]
public class WeatherForecastController : ControllerBase
{
    public WeatherForecastController()
    {
    }

    [HttpGet]
    public ActionResult<string> Test()
    {
        return "Hello";
    }
}

以下是日志(log)的输出(删除了令牌):

info: Microsoft.IdentityModel.LoggingExtensions.IdentityLoggerAdapter\[0\]
Microsoft.IdentityModel Version: 6.32.3.0. Date 10/06/2023 10:56:55. PII logging is ON, do not use in production. See https://aka.ms/IdentityModel/PII for details.
IDX10242: Security token: '{}' has a valid signature.
info: Microsoft.IdentityModel.LoggingExtensions.IdentityLoggerAdapter\[0\]
IDX10239: Lifetime of the token is valid.
info: Microsoft.IdentityModel.LoggingExtensions.IdentityLoggerAdapter\[0\]
IDX10234: Audience Validated.Audience: ''
info: Microsoft.IdentityModel.LoggingExtensions.IdentityLoggerAdapter\[0\]
IDX10245: Creating claims identity from the validated token: '{}'.
info: Microsoft.IdentityModel.LoggingExtensions.IdentityLoggerAdapter\[0\]
IDX10241: Security token validated. token: '{}'.

推荐答案

根据您的描述,当您添加[Authorize]个属性时,您得到了401个错误,而在没有该属性的情况下得到了正确的响应,所以我们可以推测这个问题与访问令牌有关.

请允许我先在Web API中共享我的配置.除了您共享的代码,我在appsettings.json中的配置如下:

"AzureAd": {
  "Instance": "https://login.microsoftonline.com/",
  "ClientId": "azure_ad_app_id",
  "ClientSecret": "client_secret",
  "Domain": "tenantId",
  "TenantId": "tenantId",
  //I used the same app to expose an API so in my sample the 2 azure_ad_app_id are the same.
  "Audience": "api://azure_ad_app_id"
},

因为我只使用了[Authorize]属性,所以无论令牌包含roles还是scope,两者都是可以的,这意味着我们拥有的令牌无论是使用身份验证码流还是客户端凭据流生成都是有效的.换句话说,如果令牌包含正确的角色或正确的SCP,则令牌有效.您可以对令牌进行解码,以判断它是否包含正确的声明.

enter image description here

让我们来看一个使用客户端凭据流令牌的示例.第一个具有应用程序类型和添加添加API权限的I exposed an api.

enter image description here

然后,我使用client credential flow来生成访问令牌.

enter image description here

有了这个令牌,我就可以调用这个API了.

enter image description here

Csharp相关问答推荐

如何在Visual Studio中为C# spread操作符设置格式规则?

如何使用C#和Graph API从Azure Directory获取用户详细信息

处理. netstandard2.0项目中HttpClient.SslProtocol的PlatformNotSupportedException问题""

在. net毛伊岛窗口的深度链接已经创建""

迭代C#List并在数据库中 for each 元素执行SELECT语句—更有效的方式?

为什么将鼠标悬停在DateTimeOffset上只显示Hour值?

在LINQ Where子句中使用新的DateTime

C#EF Core WHERE IN LINQ FROM LIST WITH.CONTAINS不返回任何内容

使用HttpResponseMessage中的嵌套列表初始化JSON

具有可空类型的C#NOTNULL约束具有意外行为

如何在不复制或使用输出的情况下定义项目依赖

同一组件的多个实例触发相同的事件处理程序

在C#中有没有办法减少大型数组中新字符串的分配?

C# Winforms:从对象树到TreeView的递归转换重复条目

KeyDown从我的文本框中删除输入,如何停止?

当`JToken?`为空时?

如何在C# WinForm控件中使用Windows 10/11的黑暗主题?

Xamarin中出错.表单:应用程序的分部声明不能指定不同的基类

C#-如何将int引用获取到byte[]

与另一个对象位于同一位置的对象具有不同的变换位置