ADAL v3:如何使用UserPasswordCredential进行身份验证? [英] ADAL v3: How to authenticate using UserPasswordCredential?

查看:202
本文介绍了ADAL v3:如何使用UserPasswordCredential进行身份验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ADAL v3具有UserPasswordCredential类,但是我找不到有效的实现。没有AcquireToken重载,它接受UserPasswordCredential或UserCredential类型。在ADAL v3中执行用户名和密码流的正确方法是什么?这段特定的代码使用的是完整的.Net 4.5。

ADAL v3 has the UserPasswordCredential class, but I cannot find a working implementation. There's no AcquireToken overload which accepts a UserPasswordCredential or UserCredential type. What is the correct way of performing the username&password flow in ADAL v3? This particular piece of code is using full .Net 4.5.

推荐答案

如果使用的是客户端应用程序,则可以引用该代码在下面获取令牌:

If you were developing with client app, you can refer the code below to acquire the token:

string authority = "https://login.microsoftonline.com/xxxx.onmicrosoft.com";
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;

如果使用Web应用程序开发(这是不常见的情况),则没有这种方法在ADAL V3中支持这种情况。解决方法是,您可以自己构造请求。以下是供您参考的示例:

And if you were developing with web app( this is not common scenario), there is no such method in ADAL V3 to support this scenario. As a workaround, you may construct the request yourself. Here is an example for your reference:

POST: https://login.microsoftonline.com/xxxxx.onmicrosoft.com/oauth2/token

Content-Type: application/x-www-form-urlencoded
resource={resource}&client_id={clientId}&grant_type=password&username={userName}&password={password}&scope=openid&client_secret={clientSecret}

这篇关于ADAL v3:如何使用UserPasswordCredential进行身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆