从Azure AD获取用户详细信息

时间:2017-03-21 07:16:36

标签: c# azure model-view-controller azure-active-directory azure-ad-graph-api

我正在尝试使用Graph API从Azure AD获取用户的详细信息。

我的代码是这样的:

public B2CGraphClient(string clientId, string clientSecret, string tenant)
    {

        this.clientId = clientId;
        this.clientSecret = clientSecret;
        this.tenant = tenant;

        this.authContext = new AuthenticationContext("https://login.microsoftonline.com/" + tenant);


        this.credential = new ClientCredential(clientId, clientSecret);
    }



public string GetUserByObjectId(string objectId)
    {
        return SendGraphGetRequest("/users/" + objectId);
    }

    public string SendGraphGetRequest(string api)
    {

        AuthenticationResult result = authContext.AcquireToken("https://graph.windows.net", credential);


        HttpClient http = new HttpClient();
        string url = "https://graph.windows.net/" + tenant + api + "?" + "api-version=1.6";

}

但是我在SendGraphGetRequest方法中获取AcquireToken行的异常为 -

Error validating credentials. Invalid client secret is provided
Inner Exception is :{"The remote server returned an error: (401) Unauthorized."} 

我提供了两者 - 客户端ID和客户端密钥,但仍然遇到此异常。

我在这里缺少什么?

2 个答案:

答案 0 :(得分:0)

根据错误消息,密码不正确。您可以在Azure门户上重新生成新密钥,并使用新密码来解决此问题。

答案 1 :(得分:0)

如果您确定该密码是正确的,请确保您已注册用作服务主体的应用程序已被分配权限:

Permissions in the portal

此特定权限要求您在授予访问权限之前获得AAD管理员的批准。

来自AAD Documentation

从目录admin

请求权限

当您准备好从组织的管理员请求权限时,您可以将用户重定向到v2.0管理员同意端点。

// Line breaks are for legibility only.

GET https://login.microsoftonline.com/{tenant}/adminconsent?
client_id=6731de76-14a6-49ae-97bc-6eba6914391e
&state=12345
&redirect_uri=http://localhost/myapp/permissions

// Pro tip: Try pasting the following request in a browser!
https://login.microsoftonline.com/common/adminconsent?client_id=6731de76-14a6-49ae-97bc-6eba6914391e&state=12345&redirect_uri=http://localhost/myapp/permissions
相关问题