【问题标题】:Why is Azure devops delegated permission asking for admin consent when the portal says it is not required?当门户网站说不需要时,为什么 Azure devops 委派权限要求管理员同意?
【发布时间】:2021-09-20 12:39:03
【问题描述】:

我正在尝试通过 Microsoft 身份 Web 库的 Itokenacquisition 获取 azure devops 令牌。当 enabletokenacquisitiontocalldownstreamapi 中存在范围时,我能够在启动文件中生成令牌并且当我已经登录到应用程序时。当我退出应用程序并再次登录时,它会要求管理员同意。不确定行为。第二次登录时,它包括 ado 范围。

【问题讨论】:

    标签: azure-devops azure-active-directory openid-connect msal asp.net-core-5.0


    【解决方案1】:

    在 Web 应用程序和 Web API 中使用令牌缓存序列化程序。它们提供 分布式数据库或缓存系统来存储令牌。

    当您使用 Microsoft Authentication Library for .NET 获取访问令牌时 (MSAL.NET),令牌被缓存。当应用程序需要令牌时, 它应该首先调用AcquireTokenSilent 方法来验证是否存在 可接受的令牌在缓存中。

    有关 AcquireTokenSilent 的详细信息,请参阅 Acquire a token from cache

    • 检查这些:
    1. 在 startup.cs 文件中确保在 ConfigureServices() 方法中添加 .AddInMemoryTokenCaches();示例:

    using Microsoft.Identity.Web;
    
    public class Startup
    {
     const string scopesToRequest = "user.read";
      
      public void ConfigureServices(IServiceCollection services)
      {
       // code before
       services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
               .AddMicrosoftIdentityWebApp(Configuration)
                 .EnableTokenAcquisitionToCallDownstreamApi(new string[] { scopesToRequest })
                    .AddInMemoryTokenCaches();
       // code after
      }
      // code after
    }
    1. 在 Configure() 方法中添加app.UseAuthentication();

    AddInMemoryTokenCaches 接受一个可选参数类型 MsalMemoryTokenCacheOptions 可让您指定持续时间 之后缓存条目将过期,除非它被使用。

    更多详情请咨询msal-net-token-cache-serialization

    1. 您可以检查您是否在身份验证请求中传递了prompt=consent 参数。您可以包含该参数。

    参考资料:

    1. How to create a .NET Core API that accepts authenticated requests from a Power App
    2. AD-permissions-and-consent
    3. permissions-and-consent#openid-connect-scopes
    4. OAuth Permissions in Azure AD & MSA

    【讨论】:

      猜你喜欢
      • 2017-04-22
      • 1970-01-01
      • 2016-10-14
      • 1970-01-01
      • 2020-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多