【问题标题】:How to implement the AAD auth provider for a web service programmatically?如何以编程方式为 Web 服务实现 AAD 身份验证提供程序?
【发布时间】:2021-04-14 14:49:37
【问题描述】:

我有一个简单的网络应用程序(网络 API),想象一下默认应用程序。我使用 AAD 身份验证提供程序 using the Azure portal 保护它,即时创建应用注册。

在“请求未通过身份验证时采取的操作”中,我选择了“使用 Azure Active Directory 登录”。所以它起作用了——租户的用户可以在 AAD 登录后访问网络。生成并提供访问令牌后,我也可以access the API via Postman

现在,我想在 API 代码中实现此 AAD 身份验证提供程序以提高灵活性(需要一些匿名端点)。怎么做?

【问题讨论】:

    标签: c# asp.net-core oauth-2.0 azure-active-directory openid-connect


    【解决方案1】:

    请查看身份验证和授权模块的 API 日志文件。

    您在 Appsetting.json 中将日志记录级别设置为调试,例如

    {
      "Logging": {
        "LogLevel": {
          "Default": "Warning",
          "Microsoft": "Warning",
          "Microsoft.Hosting.Lifetime": "Information",
          "Microsoft.AspNetCore.Authentication": "Debug",
          "Microsoft.AspNetCore.Authorization": "Debug"
        }
      }
    }
    

    此外,请检查对 API 的失败请求的响应标头并查找 WWW-Authenticate: 标头。

    如果存在,它可能如下所示:

    HTTP/1.1 401 Unauthorized
    Date: Sun, 02 Aug 2020 11:19:06 GMT
    WWW-Authenticate: Bearer error="invalid_token", error_description="The signature is invalid"
    

    【讨论】:

      【解决方案2】:

      如果您想以编程方式使用 Azure AD 身份验证保护您的 ASP.NET Core Web 应用程序,您可以参考此示例,它演示了 ASP.NET Core Web 应用程序如何从任何 Azure Active Directory(Azure AD ) 组织。

      Quickstart: Add sign-in with Microsoft to an ASP.NET Core web app

      【讨论】:

      • 感谢您的链接,@Joy。有趣的是,现在我无法使用 Postman 访问该应用程序(甚至提供有效的 Bearer 令牌)——我获得了带有“登录到您的帐户”的 HTML。这是预期的吗?那么如何解决呢?
      猜你喜欢
      • 1970-01-01
      • 2020-04-23
      • 2012-01-16
      • 2017-12-25
      • 1970-01-01
      • 2015-08-24
      • 1970-01-01
      • 2013-10-29
      • 2014-09-28
      相关资源
      最近更新 更多