c# - .net 框架的 AddMicrosoftIdentityWebApiAuthentication 模拟

标签 c# azure asp.net-web-api azure-active-directory

我有几个 Web API,其中一些使用 .Net Core 实现,其他使用 .NET 框架。

我使用 AD 和 AddMicrosoftIdentityWebApiAuthentication 来验证从一个 .NET Core 服务到另一个的响应。

谁能告诉我 .NET 框架的 AddMicrosoftIdentityWebApiAuthentication 的等效项是什么?

谢谢。

最佳答案

恐怕 UseWindowsAzureActiveDirectoryBearerAuthentication 就是您想要的。这是official sample用于 asp.net web api。

但最简单的方法应该是在创建 api 项目时集成 AAD。你将得到如下代码:

public void ConfigureAuth(IAppBuilder app)
        {
            app.UseWindowsAzureActiveDirectoryBearerAuthentication(
                new WindowsAzureActiveDirectoryBearerAuthenticationOptions
                {
                    Tenant = ConfigurationManager.AppSettings["ida:TenantId"],
                    TokenValidationParameters = new TokenValidationParameters {
                         ValidAudience = ConfigurationManager.AppSettings["ida:Audience"]
                    },
                });
        }

enter image description here

我进行了测试,这是测试结果。我刚刚添加了 AAD 的配置。在我的 web.config 文件中,我有以下内容:

<configuration>
  <appSettings>
      <add key="ida:Tenant" value="tenant_id" />
      <add key="ida:TenantId" value="tenant_id" />
      <add key="ida:Audience" value="api://aad_client_id_used_exposing_api" />
      <add key="ida:ClientID" value="aad_client_id" />
      <add key="ida:AppKey" value="client_secret" />
      <add key="ida:AADInstance" value="https://login.microsoftonline.com/{0}" />
      <add key="ida:RedirectUri" value="https://localhost:44321/" />
      <add key="ida:GraphUserUrl" value="https://graph.microsoft.com/v1.0/me/" />
  </appSettings>

enter image description here

我通过此请求生成的 token 。

enter image description here

关于c# - .net 框架的 AddMicrosoftIdentityWebApiAuthentication 模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75585354/

相关文章:

c# - 使用外部 Web API 提供的 OAuth Bearer Token 进行授权

asp.net-web-api - 没有 'MediaTypeFormatter' 可用于读取类型 'String' 的对象

c# - ASP.NET WEB API 启动类

c# - 是否可以将 Windows 语言栏设置为英语或从 c# 应用程序恢复为默认设置

c# - 如何使用 ORMlite 将 byte[] 数组插入图像列

c# - 网。辐射网格。在回发时丢失更改的值

azure - 数据库 DBFS

c# - 调用链中的异步等待分配

c# - 一个队列上的多个 Azure Webjob 实例(第 2 部分)

azure - 机器人抛出异常时没有日志