繁体   English   中英

.Net 客户端应用程序 Azure AD 身份验证 Cookie 身份验证然后 Bearer token 调用下游 API

[英].Net Client App Azure AD Authentication Cookie Authentication And then Bearer token to call downstream API

我正在使用 Azure AD 身份验证 Web Forms 应用程序以及 ASP.NET MVC(客户端应用程序)Framework 4.8。

我使用 Owin Startup class 将 cookie 身份验证配置到 Azure AD。

app.UseCookieAuthentication(cookieAuthOpts)

现在我需要使用 httpclient 和 soap 客户端调用下游 API(rest 和 SOAP),并且想了解最好的方法,因为在 httpclient/soap 客户端中将现有身份验证 cookie 添加到 header 似乎不是一个选项。 任何例子都会很棒。

用户已经使用 azure 广告 OpenIdConnectAuthenticationOptions 登录,因此让用户在调用下游 api 之前再次登录似乎不是一个选项。

解决方法之一是使用启动 class 中Microsoft.Identity.Web.AddDownstreamWebApi() ,它请求令牌并调用下游 web API。

using Microsoft.Identity.Web;

public class Startup
{
  // ...
  public void ConfigureServices(IServiceCollection services)
  {
  // ...
  services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
          .AddMicrosoftIdentityWebApi(Configuration, "AzureAd")
            .EnableTokenAcquisitionToCallDownstreamApi()
               .AddDownstreamWebApi("MyApi", Configuration.GetSection("GraphBeta"))
            .AddInMemoryTokenCaches();
   // ...
  }
  // ...
}

参考: 配置调用web API的web API

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM