【问题标题】:Microsoft Identity Web : Azure AD Client Credential flow with Certificate Based AuthenticationMicrosoft Identity Web:具有基于证书的身份验证的 Azure AD 客户端凭据流
【发布时间】:2022-01-24 15:21:07
【问题描述】:

我正在使用 Microsoft Identity Web (MSAL) 库连接到 Graph API。 [https://github.com/AzureAD/microsoft-identity-web][1]

为此,我将客户端凭据流与基于证书的身份验证一起使用。

我的配置如下

服务注册

  services.AddMicrosoftIdentityWebApiAuthentication(Configuration)
           .EnableTokenAcquisitionToCallDownstreamApi()
           .AddMicrosoftGraph(Configuration.GetSection("DownstreamApi"))
           .AddInMemoryTokenCaches();

appSettings.json

"AzureAd": {
    "Instance": "https://login.microsoftonline.com/",
    "Domain": "mydomain.onmicrosoft.com",
    "TenantId": "xxxxxxx",
    "ClientId": "yyyyyyyy",   
    "ClientCertificates": [
     {
       "SourceType": "Path",
       "CertificateDiskPath": "c:\\cert\\my-cert.pfx",
       "CertificatePassword": "password"
     }
 ] }

为此,我收到以下错误

IDW10104:客户端密码和客户端证书都不能为空或 空格,并且只有 ONE 必须包含在 调用 Web API 时的 Web 应用程序。例如,在 appsettings.json 文件。

但是,我能够使用 Microsoft.Identity.Client 累积令牌并连接到 Graph API(使用基于证书的身份验证的客户端凭据流)

    private GraphServiceClient GetGraphServiceClient()
    {
        var token = GetToken();
        GraphServiceClient graphServiceClient =
            new GraphServiceClient(new DelegateAuthenticationProvider(async (requestMessage) =>
            {
                // Add the access token in the Authorization header of the API request.
                requestMessage.Headers.Authorization =
                        new AuthenticationHeaderValue("Bearer", token);
            })
            );
        return graphServiceClient;
    }

    private string GetToken()
    {
        var x509Certificate2 = 
            new X509Certificate2(System.IO.File.ReadAllBytes("MyCert.pfx"), "password");

        IConfidentialClientApplication app = 
            Microsoft.Identity.Client.ConfidentialClientApplicationBuilder.Create("my-client-id")
                .WithTenantId("my-tenent-id")
                .WithCertificate(x509Certificate2)
                .Build();

        // With client credentials flows the scopes is ALWAYS of the shape "resource/.default", as the
        // application permissions need to be set statically (in the portal or by PowerShell), and then granted by
        // a tenant administrator
        string[] scopes = new string[] { "https://graph.microsoft.com/.default" };
         
        AuthenticationResult result =
                    app.AcquireTokenForClient(scopes)
                        .ExecuteAsync().Result;
        return result.AccessToken;
    }

我在这里缺少任何配置吗?

【问题讨论】:

    标签: azure azure-active-directory msal microsoft-identity-web clientcredential


    【解决方案1】:

    关于解决方法

    尝试在 Azure 应用注册中添加证书

    1) 转至Azure portal。在左侧导航窗格中,选择 Azure Active Directory 服务,然后选择应用注册。

    2)在结果屏幕中,选择选择您的应用程序。

    3)在证书和机密标签中,转到证书部分:

    4) 选择上传证书,然后在右侧的浏览按钮中选择您现有的证书。

    5) 选择添加。

    更多详情请参考此文档:https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/blob/master/4-WebApp-your-API/4-1-MyOrg/README-use-certificate.md

    【讨论】:

    • 我希望不是真的。因为当我使用 => services.AddMicrosoftIdentityWebApiAuthentication(Configuration) 时,来自 appSettings.json 的 ClientCertificates 值没有绑定
    猜你喜欢
    • 2023-01-14
    • 1970-01-01
    • 2021-04-05
    • 2012-01-03
    • 2017-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-03
    相关资源
    最近更新 更多