【问题标题】:Authentication flows when a web app authenticate visitors and itself towards Microsoft Graph当 Web 应用程序向 Microsoft Graph 对访问者及其自身进行身份验证时,身份验证流程
【发布时间】:2019-07-14 16:02:07
【问题描述】:

我正在尝试构建一个小型 Web 应用程序,该应用程序将使用 Azure AD 对访问用户进行身份验证,然后将用户添加到 Azure AD 中的指定组。使用的组件是 C#/dotnet core、MSAL 和 .NET 的 Microsoft Graph 库。

步骤很简单:

  1. 用户访问网站。
  2. 用户通过 Azure AD 的身份验证 带有 OpenID 连接。
  3. 成功通过身份验证后,网站 使用 Microsoft 将用户添加为特定 Azure AD 组中的成员 图形 API。
  4. 用户会看到操作的状态。

应用程序在 Azure AD 中注册,具有隐式授权(用于 ID 令牌)并具有以下 Azure AD 权限:

  • Microsoft Graph:Group.ReadWrite.All
  • Microsoft Graph:User.Read.All

控制器如下所示:

    public async Task<string> Test()
    {
        //get authenticated user
        var identity = User.Identity as ClaimsIdentity;
        string preferred_username = identity.Claims.FirstOrDefault(c => c.Type == "preferred_username")?.Value;

        //get appsettings.json
        var azureAdOptions = new AzureADOptions();
        _configuration.Bind("AzureAd", azureAdOptions);

        //do Microsoft Graph stuff
        GraphServiceClient graphClient = new GraphServiceClient(new DelegateAuthenticationProvider(
            async requestMessage =>
            {
                string authority = $"{azureAdOptions.Instance}{azureAdOptions.TenantId}";

                ClientCredential clientCredentials = new ClientCredential(azureAdOptions.ClientSecret);

                var app = new ConfidentialClientApplication(azureAdOptions.ClientId, authority, "https://daemon",
                                                            clientCredentials, null, new TokenCache());

                string[] scopes = new string[] { "https://graph.microsoft.com/.default" };

                // Passing tenant ID to the sample auth provider to use as a cache key
                AuthenticationResult authResult = null;
                authResult = await app.AcquireTokenForClientAsync(scopes);

                // Append the access token to the request
                requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
            }));

        User userToAdd = await graphClient.Users[preferred_username].Request().GetAsync();
        await graphClient.Groups["c388b7a4-2a22-4e3f-ac11-900cef9f74c6"].Members.References.Request().AddAsync(userToAdd);

        return $"added {userToAdd.DisplayName} to group";
    }

Startup.cs 如下所示:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
            .AddAzureAD(options => Configuration.Bind("AzureAd", options));

        services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, options =>
        {
            options.Authority = options.Authority + "/v2.0/";
            options.TokenValidationParameters.ValidateIssuer = true;
        });

        services.AddMvc(options =>
        {
            var policy = new AuthorizationPolicyBuilder()
                .RequireAuthenticatedUser()
                .Build();
            options.Filters.Add(new AuthorizeFilter(policy));
        })
        .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseAuthentication();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }

使用此代码,我有两个身份验证流程。一种用于对访问用户进行身份验证,另一种用于对 Microsoft Graph 的应用程序进行身份验证。这背后的基本原理是用户没有足够的权限将成员添加到组。该代码可以正常工作并且完全符合预期。

针对单个 Azure AD 应用注册的双重身份验证流程是确保实现这一目标的最佳方式,还是存在只需要一个身份验证流程的更时尚的设计?

【问题讨论】:

    标签: asp.net-core azure-active-directory openid-connect msal microsoft-graph-sdks


    【解决方案1】:

    据我所知,您确实需要支持这两种流程。您的用户需要一个令牌来与您的 Web 应用程序对话,而您的 Web 应用程序需要一个不同的令牌来与 Graph 对话。

    希望您很快就不需要 DelegateAuthenticationProvider 中的所有代码,因为我们将很快预览一堆基于场景的 AuthenticationProvider。 ClientCredentialProvider 应该为您完成所有工作。

    【讨论】:

    • 在 Azure AD 中通过单个应用注册来支持两个流是否被认为是不好的做法(或不安全),即是否应该在 Azure AD 中将两个流注册为两个单独的应用?
    • @agnsaft 我相信没问题。您的应用程序同时充当资源所有者和资源使用者。这是非常自然的。
    • 是否可以通过使用代表流而不是客户端凭据流来实现相同的目的,如果是这样,用户对象是否必须被授予额外的权限,或者添加额外的委托就足够了应用权限?
    • 是的,您可以使用代理流程。您关于用户是否必须同意的问题是一个很好的问题。我会调查的。
    • 实际上,与其说是同意,不如说是整个用户对象必须具有足够的权限(例如,上例中的组所有者),或者是否在获得管理员同意的情况下委派权限应用程序将确保用户自己不需要这些权限。
    猜你喜欢
    • 1970-01-01
    • 2017-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-23
    • 2014-01-26
    • 2011-06-27
    • 2019-10-13
    相关资源
    最近更新 更多