将用户凭据传递给Adal AquireTokenAsync

时间:2017-02-08 10:58:21

标签: mobile active-directory passwords credentials adal

我使用的是Adal(Active Directory身份验证库)版本3.13.x.我正在做类似下面的事情来获取访问令牌

AuthResult = await AuthContext.AcquireTokenAsync(relyingUrl, ServiceConstants.CLIENTID, ServiceConstants.RETURNURI, param);

我也需要传递UserCredentials,但是现在我只能将一个参数传递给UserCredential(),这与Adal的Version 2.x.x不同。

我也尝试使用this解决方案中建议的UserPasswordCredential,但由于我想从移动应用中获取令牌,因此我只能访问.net Core,因此无法使用{{ 1}}

我想在获取令牌时一起传递用户名和密码。有什么方法可以实现这个目标吗?

任何帮助都将不胜感激。

修改

从飞雪 - MSFT试用解决方案后,我得到以下503错误。

enter image description here

1 个答案:

答案 0 :(得分:1)

  

我想在获取令牌时一起传递用户名和密码。有什么方法可以实现这个目标吗?

在这种情况下,我们可以直接执行REST请求,而不是使用客户端库。以下是供您参考的示例:

Post:https://login.microsoftonline.com/{tenant}/oauth2/token

resource={resource}&client_id={clientId}&grant_type=password&username={userName}&password={password}

更新

string authority = "https://login.microsoftonline.com/{tenantid}";
string resrouce = "https://graph.windows.net";

string clientId = "";
string userName = "";
string password = "";
UserPasswordCredential userPasswordCredential = new UserPasswordCredential(userName, password);
AuthenticationContext authContext = new AuthenticationContext(authority);
var token = authContext.AcquireTokenAsync(resrouce, clientId, userPasswordCredential).Result.AccessToken;

Console.WriteLine(token);
相关问题