【问题标题】:Connecting with ADAL to Microsoft Graph gives 406 error使用 ADAL 连接到 Microsoft Graph 会出现 406 错误
【发布时间】:2016-11-01 16:22:10
【问题描述】:

我正在尝试在 C# 客户端中获取 ActiveDirectoryClient,如下所示:

 Uri servicePointUri = new Uri("https://graph.microsoft.com/v1.0/me/messages");
 Uri serviceRoot = new Uri(servicePointUri, <OUR-AZURE-TENANT-ID>);
ActiveDirectoryClient activeDirectoryClient = new ActiveDirectoryClient(serviceRoot,
                    async () => await AcquireTokenAsyncForUser());

使用这个 AcquireTokenAsyncForUser() 方法:

public static async Task<string> AcquireTokenAsyncForUser()
    {
        return await GetTokenForUser();
    }


    public static async Task<string> GetTokenForUser()
    {
        if (TokenForUser == null)
        {

            AuthenticationContext authenticationContext = new AuthenticationContext("https://login.microsoftonline.com/common/v2.0");
            UserPasswordCredential userCredential = new UserPasswordCredential("<USERNAME>@outlook.com", <PASSWORD>);

            AuthenticationResult userAuthnResult = await authenticationContext.AcquireTokenAsync("https://graph.microsoft.com/v1.0/me/messages",
                <AZURE AD APP CLIENT ID>, userCredential);

            TokenForUser = userAuthnResult.AccessToken;
            Console.WriteLine("\n Welcome " + userAuthnResult.UserInfo.GivenName + " " +
                              userAuthnResult.UserInfo.FamilyName);
        }
        return TokenForUser;
    }

我不断收到此错误:

获取登录用户时出错 access_ws_metadata_exchange_failed: 访问 WS 元数据交换失败-

响应状态码不表示成功:406(NotAcceptable)。-

我使用正确或错误的凭据都没有关系。

【问题讨论】:

    标签: c# adal http-status-code-406


    【解决方案1】:

    AAD 不支持 MSA 帐户的 WS-Trust 登录。您必须通过调用

    通过 webview 登录用户
    AcquireTokenAsync("https://graph.microsoft.com/v1.0/me/messages",
                    <AZURE AD APP CLIENT ID>, new Uri("<your redirect uri>", new PlatformParameters(PromptBehavior.Auto{or whatever you want}, null));
    

    【讨论】:

    • 感谢您的回复,当运行此代码的设备上没有浏览器/UI 时我该怎么办?我应该考虑使用 Office365 帐户吗?
    • 您可以使用设备代码流程演示T cloudidentity.com/blog/2015/12/02/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-25
    • 1970-01-01
    • 2014-08-14
    • 1970-01-01
    • 1970-01-01
    • 2020-07-19
    相关资源
    最近更新 更多