如何使用OAuth2和microsoft登录和HTTP请求使用用户名/密码登录

时间:2016-12-09 10:16:47

标签: azure login oauth active-directory oauth-2.0

我自己回答这个问题,让我们的开发人员找到这些信息变得更轻松,更快乐,因为我个人发现有很多信息可以通过。

如何使用OAuth2和Microsoft登录使用用户名/密码(最终用户凭据)登录 https://login.microsoftonline.com/ {应用ID} /的oauth2 /令牌

1 个答案:

答案 0 :(得分:7)

Microsoft建议不要为最终用户传递用户名/密码信息。这就是为什么他们不为此发布指南。出于安全原因,他们希望您使用重定向到他们的登录页面。使用https://login.microsoftonline.com/ {tenant-id} / oauth2 / authorize到达那里。

然而,使用他们的oauth2 rest api可以很容易地做到这一点。

创建

的http请求
base url: https://login.microsoftonline.com/{tenant-id}/oauth2/token
{tenant-id} //obtained from AzureAD config section

在正文部分

中使用以下请求参数
grant_type = password //read up on the other grant types, they are all useful, client_credentials and authorization_code
client_id = {client-id}//obtained from the application section in AzureAD
client_secret = {client-secret}//obtained from the application section in AzureAD
resource = https://graph.microsoft.com //there is also the api https://graph.windows.net, use the latest and best one
username = {enduser-username} //example rofler@domain.onmicrosoft.com
password = {enduser-password} //example Hs782f8a

成功的回复应该包括access_token和refresh_token

2016年测试

推荐链接