UseWindowsAzureActiveDirectoryBearerAuthentication 与 UseOpenIdConnectAuthentication 之间有什么区别?

Neo*_*Neo 2 .net c# azure azure-active-directory

我有已注册到 azure AD 的 webapi。Startup.Auth.cs 中有以下代码

   public void ConfigureAuth(IAppBuilder app)
        {
            app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
            app.UseCookieAuthentication(new CookieAuthenticationOptions());
            app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
            {
                ClientId = ClientId,
                Authority = Authority,
                PostLogoutRedirectUri = PostLogoutRedirectUri
            });
Run Code Online (Sandbox Code Playgroud)

当我在浏览器中运行此 webapi 时,它要求登录,登录成功后能够看到所有 api 的 url。

我正在尝试从注册到同一天蓝色 AD 的网络应用程序访问此安全 api。

当我使用AcquireTokenAsyncWebapp 为该 webapi 生成访问令牌时,它可以工作,但会提供登录 html 页面作为响应。

为了避免这种情况,我尝试使用生成静默令牌,AcquireTokenSilentAsync但发生异常unable to generate token cache not found,但缓存密钥发现仍然存在异常。

在一些 google git 帖子建议将此代码使用UseWindowsAzureActiveDirectoryBearerAuthentication到 WebApi 中之后,他们说当从 webapp 调用时它将返回 api 的输出而不是登录页面输出,但它不起作用。

 app.UseWindowsAzureActiveDirectoryBearerAuthentication(
                new WindowsAzureActiveDirectoryBearerAuthenticationOptions
                {
                    Audience = ConfigurationManager.AppSettings["ida:Audience"],
                    Tenant = ConfigurationManager.AppSettings["ida:Tenant"],
                    TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters
                    {
                        ValidateIssuer = false
                    }
                });
Run Code Online (Sandbox Code Playgroud)

juu*_*nas 5

API 不应要求用户登录。持有者令牌身份验证的第二个选项是正确的。这意味着您的客户端应用程序需要获取访问令牌并将其与 HTTP 请求一起传递。

AcquireTokenAsync并且AcquireTokenSilentAsync工作方式与您的想法有些不同。第一个接受一些参数,然后调用 AAD 权限的 /oauth2/token 端点来获取访问令牌,除非它在缓存中已经有访问令牌。静默版本仅检查缓存,如果找不到缓存则抛出异常。

因此,您的客户端应用程序通常会使用其中一种AcquireTokenAsync变体来获取访问令牌和刷新令牌。当您执行此操作时,ADAL 会将令牌存储在您提供的令牌缓存中(默认情况下是内存缓存)。然后,稍后在代码中,您可以使用静默版本来获取令牌,因为您可以期望它们位于缓存中。