c# - UseWindowsAzureActiveDirectoryBearerAuthentication 与 UseOpenIdConnectAuthentication 之间有什么区别?

标签 c# .net 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
            });

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

我正在尝试从注册到同一 azure AD 的 Web 应用程序访问此安全 api。

当我在 Webapp 中使用 AcquireTokenAsync 来生成此 webapi 的访问 token 时,它可以工作,但会提供登录 html 页面作为响应。

为了避免这种情况,我尝试使用AcquireTokenSilentAsync生成静默 token ,但发生异常无法生成未找到的 token 缓存但缓存 key 发现仍然存在异常。

在一些 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
                    }
                });

最佳答案

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

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

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

关于c# - UseWindowsAzureActiveDirectoryBearerAuthentication 与 UseOpenIdConnectAuthentication 之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52801758/

相关文章:

azure - azure-pipelines 中的 apt-get 安装随机失败

azure - 逻辑应用程序,如何使用 Liquid 转换 JSON 数组

c# - 如何让 AutoMapper 不缓存映射对象?

c# - Json 序列化将 k__BackingField 添加到我的属性

java - .NET 中的严格 float 学库

c# - PE格式自检

c# - 使用显式 TLS/SSL 的 FtpWebRequest

c# - 输入字符串包含非 ASCII 或空字符

c# - 如何按相同权重的多个参数进行排序?

python - 如何通过 Python SDK 设置 azure 容器应用程序的身份验证选项?