gpt4 book ai didi

azure - 为什么 Microsoft Identity Web API 中间件拒绝对访问 token 进行身份验证?

转载 作者:行者123 更新时间:2023-12-03 06:17:49 25 4
gpt4 key购买 nike

我在验证 ASP.NET Core Web api 时遇到问题。

我有一个使用 React + Fable 制作的 SPA 客户端。使用 Msal,我毫无问题地获取了访问 token ,并将其在 Authorization header 内发送到 Web API。具体来说,我使用托管 SPA 前端的 Vite 开发服务器来代理从前端到后端的请求。

  1. 由于我拥有来自 Azure AD 的 token ,我是否可以假设客户端一切正常?

  2. 这是我的 appsettings.json 的内容。

    "AzureAd": {
"Instance": "https://login.microsoftonline.com/",
"Domain": "simple-login-page.com",
"TenantId": "ec88369d-6c2f-4f15-b0c7-adbe35caec77",
"ClientId": "fd12b58b-6c39-4f86-ba02-98e6d7ee5eb5",
"CallbackPath": "/signin-oidc"
},

我在这里遗漏了什么吗?我已验证 ID 与客户端中的 ID 匹配。

  • 这是我的服务器 Program.cs
  • [<EntryPoint>]
    let main _ =
    let builder = WebApplication.CreateBuilder(WebApplicationOptions(
    WebRootPath = "public"
    ))

    builder.Services
    .AddGiraffe()
    .AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddMicrosoftIdentityWebApi(builder.Configuration.GetSection "AzureAd")
    |> ignore

    let app = builder.Build()

    app
    .UseAuthentication()
    .Use(Func<HttpContext,RequestDelegate,Task>(fun ctx next -> task {
    if ctx.User.Identity.IsAuthenticated then return! next.Invoke(ctx)
    else return! ctx.ForbidAsync()
    }))
    .UseFileServer()
    .UseGiraffe(webApp)

    app.Run()

    0 // Exit code

    每当服务器收到请求时,我都会收到一条错误消息,指出某些 key 的验证失败。我在这里遗漏了什么吗?

    我是否需要在 Azure AD 上配置某些内容以允许服务器使用它?这是the full project .

    最佳答案

    事实证明,与 Web 应用不同,SPA + Web API 需要不同的 Azure AD 设置。 Web API 需要公开,需要有一个范围,并且 SPA 需要链接到该范围。您还需要在重定向登录期间请求该范围。

    我做了一个video展示如何从头到尾设置 SPA 登录。

    关于azure - 为什么 Microsoft Identity Web API 中间件拒绝对访问 token 进行身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76078452/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com