是否可以使用MS Graph REST API在代码中登录用户并获取身份验证令牌?

是否可以使用MS Graph REST API在代码中登录用户并获取身份验证令牌?

问题描述:

我真的是在这样的事情之后:https://github.com/Azure-Samples/active-directory-dotnet-native-headless它曾经在3.9版本中工作,但现在不是。是否可以使用MS Graph REST API在代码中登录用户并获取身份验证令牌?

具体来说: authContext.AcquireTokenAsync(todoListResourceId,clientId,uc).Result;

那么使用REST API可以做同样的事情吗?

这是一个功能测试,所以我们不能让用户正常登录。我需要复制登录后用户会看到的内容。

+0

就像这样:http://*.com/a/41159005/1658906? – juunas

是的。可以使用HTTP请求直接获取令牌资源所有者密码凭证流程

我们可以使用Fiddler捕获请求以查看所需的详细参数。这里是一个代码示例使用这个流程来获得访问令牌的原生客户端应用程序:

string resrouce = "https://graph.windows.net"; 
string clientId = ""; 
string userName = ""; 
string password = ""; 

HttpClient client = new HttpClient(); 
string body = String.Format("resource={0}&client_id={1}&grant_type=password&username={2}&password={3}", Uri.EscapeDataString(resrouce), clientId, Uri.EscapeDataString(userName), Uri.EscapeDataString(password)); 
StringContent sc = new StringContent(body,Encoding.UTF8, "application/x-www-form-urlencoded"); 
var resoult= client.PostAsync("https://login.microsoftonline.com/xxxx.onmicrosoft.com/oauth2/token", sc).Result.Content.ReadAsStringAsync().Result; 

如果您正在使用的网络应用程序,我们需要将client_secret参数附加在身上。