【问题标题】:Fetching user based access token using grant type as password使用授权类型作为密码获取基于用户的访问令牌
【发布时间】:2020-03-04 19:01:31
【问题描述】:

我正在尝试使用用户凭据检索访问令牌。

我正在使用 AcquireTokenAsync 方法来检索令牌,其中我使用带有资源、客户端 ID 和用户凭据作为参数的构造函数。

public async Task<IHttpActionResult> GetToken()
    {
        AuthenticationResult authenticationResult = null;
        try
        {
            string authority = "https://login.microsoftonline.com/tenant";
            string resource ="2424-234-234234-234-23-32423";
            string username = "yxyzzz";
            string password = "password";
           string clientId="2424-234-234234-234-23-32423";
            var useridpassword = new UserPasswordCredential(username, password);
            AuthenticationContext context = new AuthenticationContext(authority);
            context.TokenCache.Clear();
            authenticationResult = await context.AcquireTokenAsync(resource, clientId, useridpassword);
            return authenticationResult.AccessToken;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

我希望返回访问令牌,但在获取令牌时出现异常。以下是我收到的错误消息。

AdalException: {"error":"invalid_client","error_description":"AADSTS7000218: The request body must contain the following parameter: 'client_assertion' or 'client_secret'.\r\nTrace ID: 674f29fe-73c6-49a3-9c3f-24df4ea16000\r\nCorrelation ID: b14cb535-9df5-48fa-b911-7e8b927fceb7\r\nTimestamp: 2019-11-08 06:21:57Z","error_codes":[7000218],"timestamp":"2019-11-08 06:21:57Z","trace_id":"674f29fe-73c6-49a3-9c3f-24df4ea16000","correlation_id":"b14cb535-9df5-48fa-b911-7e8b927fceb7","error_uri":"https://login.microsoftonline.com/error?code=7000218"}: Unknown error

【问题讨论】:

  • 这个问题有什么更新吗?
  • 是的.. 可以通过调用 Oauth2/token 端点来获取令牌。
  • 我已经分享了对我来说很好用的代码。
  • 感谢您的分享,您可以将其作为答案。那么这个issue就可以关闭了。谢谢。

标签: asp.net azure access-token azure-authentication


【解决方案1】:

要使用资源所有者密码凭证,您需要将应用程序视为公共客户端。

转到 azure 门户->应用注册->找到您的应用->检查高级设置

【讨论】:

    【解决方案2】:

    这是我用来获取令牌的代码。这就是我想要检索访问令牌的内容。

            string authority = "https://login.microsoftonline.com/tenant";
            string resource ="2424-234-234234-234-23-32423";
            string username = "yxyzzz";
            string password = "password";
           string clientId="2424-234-234234-234-23-32423";
                string tokenEndpointUri = authority + "/oauth2/token";
                var content = new FormUrlEncodedContent(new[]
                    {
            new KeyValuePair<string, string>("grant_type", "password"),
            new KeyValuePair<string, string>("username", username),
            new KeyValuePair<string, string>("password", password),
            new KeyValuePair<string, string>("client_id", authmodel.ClientId),
            new KeyValuePair<string, string>("client_secret", authmodel.ClientSecret),
            new KeyValuePair<string, string>("resource", resource)
        }
                );
                using (var client = new HttpClient())
                {
                    HttpResponseMessage res = null;
                        client.PostAsync(tokenEndpointUri, content).
                        ContinueWith(t =>
                        {
                            try
                            {
                                res = t.Result;
                            }
                            catch (Exception ex)
                            {
                                throw ex;
                            }
                        })
                        .Wait();
    
                    string json = await res.Content.ReadAsStringAsync();
    
                }
    

    我在 json 变量中获取访问令牌以及其他详细信息。如果要获取token值,可以反序列化为.net Object并获取值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-08-16
      • 1970-01-01
      • 2012-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-05
      相关资源
      最近更新 更多