English 中文(简体)
。 互联网信息预报可以通过经授权的客户识别/授权的自动接收器
原标题:.NET API can t recognize/authorize Azure AD user through authorized client s JWT

我正试图通过我的“网络8”批准“积极名录”用户进入下一个JS客户申请(使用msal-react),以便他们能够使用[Authorize]终端。 然而,在将客户JWT送至我的APIC时,我收到了401份答复和我的APIC标志

I have registered both applications through Azure s App registrations. In my API registration, I have exposed my API to authorize my client app through a single scope, access_as_user. I have confirmed this in my client s registration as a Delegated permission. Azure AD App registration screen showing the Expose an API page

“API对我的APIC/>>

API permissions for my client app

Showing I m granting access and ID tokens

在我的“网络”中,Im打上了我的<代码>AzureAd配置,载于appdings.json

"AzureAd": {
  "Instance": "https://login.microsoftonline.com/",
  "Domain": "qualified.domain.name",
  "TenantId": "My-TENANT-ID",
  "ClientId": "MY-CLIENT-ID",
  "Scopes": "access_as_user",
  "CallbackPath": "/signin-oidc"
},

I then add authentication to my services in my Program.cs

...
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddMicrosoftIdentityWebApi(config.GetSection("AzureAd"));
...
app.UseAuthorization();
app.UseAuthentication();
...

最后,我设立了单一<代码>的简单控制器。

[Controller]
[Route("api")]
public class GeneralController : Controller
{
    [Authorize]
    [HttpGet("get")]
    public Task<IActionResult> Test()
    {
        return Task.FromResult<IActionResult>(Ok());
    }
}

In my client application, I retrieve my token after a user logs in with Azure AD.

const { instance, accounts } = useMsal();
...
instance.acquireTokenSilent({
   scopes: [
     "api://API-CLIENT-ID/access_as_user",
   ],
     account: account,
})

I m 能够记录客户检索的标语,并在使用https://jwt.io/时, 我可以将《维也纳条约法公约》编码,以找到以下相关特性:

{
  "aud": "API-CLIENT-ID",
  "iss": "https://login.microsoftonline.com/CLIENT-TENANT-ID/v2.0",
  "azp": "CLIENT-CLIENT-ID",
  "name": "LOGGED-IN-USER-NAME",
  "scp": "access_as_user",
  "tid": "CLIENT-TENANT-ID",
  "ver": "2.0"
}

However, when I include this token in a request s Authorization header to the general endpoint I set up, I receive a 401 response and the following logs from my API:

info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[2]
      Authorization failed. These requirements were not met:
      DenyAnonymousAuthorizationRequirement: Requires an authenticated user.
info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[12]
      AuthenticationScheme: Bearer was challenged.
问题回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签