【问题标题】:not able to get the token from azure active directory by pass username and password for gmail user无法通过传递 gmail 用户的用户名和密码从 azure Active Directory 获取令牌
【发布时间】:2020-06-09 17:51:27
【问题描述】:

我正在尝试以下方法从 azure 活动目录获取访问令牌

我在 c sharp 中的代码如下

   public async Task<string> validateADPCredential(string data)
    {
        data = HttpUtility.UrlDecode(data);
        ADPCredential adpCredential = JsonConvert.DeserializeObject<ADPCredential>(data);

        try
        {
            string clientId = ConfigurationManager.AppSettings["azureclient_id"];
            string clientSecret = ConfigurationManager.AppSettings["azureclient_secret"];
            string tenant = ConfigurationManager.AppSettings["tenantId"];
            string resource = ConfigurationManager.AppSettings["resource"];

            var authority = "https://login.microsoftonline.com/" + tenant;


            var azureAdEndpoint = new Uri("https://login.microsoftonline.com/eshaweb.onmicrosoft.com/oauth2/v2.0/token");
            var urlEncodedContent = new FormUrlEncodedContent(new[]
            {
               new KeyValuePair<string, string>("grant_type", "password"),


               new KeyValuePair<string, string>("client_id", clientId), //using the api client id
               new KeyValuePair<string, string>("username", adpCredential.Username),
               new KeyValuePair<string, string>("password", adpCredential.Password),
               new KeyValuePair<string, string>("client_secret", clientSecret),
               new KeyValuePair<string, string>("scope", "https://graph.microsoft.com/.default"),

        });

            HttpClient httpClient = new HttpClient();
            var result = await httpClient.PostAsync(azureAdEndpoint, urlEncodedContent);
            {
                var content = await result.Content.ReadAsStringAsync();
                var authResult = JsonConvert.DeserializeObject<dynamic>(content);
                return authResult.access_token;
            }
        }
        catch (Exception ex)
        {

        }
        return string.Empty;

    }

此代码工作正常,我能够为活动目录的内部用户获取令牌。但对于 gmail 用户(外部用户),我收到以下错误。

{{

"error": "invalid_grant", "error_description": "AADSTS50034: esheb.onmicrosoft.com 目录中不存在用户帐户 {EmailHidden}。要登录此应用程序,必须将帐户添加到目录中。\r\n跟踪 ID: 0222e890-a19e- 4694-a004-327f2312aa00\r\n相关 ID:8587e63d-9cd-4257-aa6c-1ef394d03f56\r\n时间戳:2020-06-09 16:57:03Z", “错误代码”:[ 50034 ], "时间戳": "2020-06-09 16:57:03Z", "trace_id": "0222e90-a19e-4694-a004-327f2312aa00", “correlation_id”:“8587e63d-c6d-4257-aa6c-1ef394d03f56”, "error_uri": "https://login.microsoftonline.com/error?code=50034" }}

如果我尝试通过以下方法获取令牌

https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow

我能够获取天蓝色活动目录的内部和外部用户的令牌。

谁能帮我找出解决办法。

【问题讨论】:

    标签: c# azure-ad-graph-api


    【解决方案1】:

    gmail用户(外部用户)不支持使用ROPC,您可以找到如下文档。

    受邀加入 Azure AD 租户的个人帐户无法使用 ROPC(资源所有者密码凭证)。

    参考:

    Microsoft identity platform and OAuth 2.0 Resource Owner Password Credentials

    【讨论】:

    • 但是使用验证码方法我也可以获得gmail的令牌
    • @rakshithrai 是的,身份验证代码流程是另一个流程。这不仅适用于 ropc 流。
    • @rakshithrai 你想用令牌做什么?您还可以使用不需要用户交互的客户端凭据流。 docs.microsoft.com/en-us/graph/auth-v2-service
    • @rakshithrai 如果您必须代表用户 (docs.microsoft.com/en-us/graph/auth-v2-user) 获得访问权限,则外部用户的唯一方法是使用身份验证代码流。
    • @rakshithrai 看到上面的评论,获取访问令牌有两种方式,一种给应用程序,一种给用户。对于客户端凭证流(无需用户交互),应用程序将能够访问所有用户。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-25
    • 2018-01-05
    • 2018-02-17
    • 2023-01-26
    • 1970-01-01
    • 1970-01-01
    • 2018-07-09
    相关资源
    最近更新 更多