我正在尝试使用下面提到的代码从Web应用程序中的AzureAD获取所有office365用户的列表。但是,authContext.AcquireTokenAsync(resrouce,clientCredential)永远不会返回控件。我已经尝试过以下用于控制台应用程序的代码,它运行良好。但是,我很想知道为什么代码无法在Web上运行,或者我需要进行哪些修改才能使代码在Web上运行。
public static async Task<string> AcquireMyToken()
{
string clientId = "";
string secrect = "";
string resrouce = "https://graph.microsoft.com";
string authority = "https://login.microsoftonline.com/tenanId";
AuthenticationContext authContext = new AuthenticationContext(authority);
ClientCredential clientCredential = new ClientCredential(clientId, secrect);
AuthenticationResult authResult = await authContext.AcquireTokenAsync(resrouce, clientCredential);
return authResult.AccessToken;
}
public static async void ListFiles(string accessToken)
{
var graphClient = new GraphServiceClient(new DelegateAuthenticationProvider(
(requestMessage) =>
{
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
return Task.FromResult(0);
}));
var users = await graphClient.Users.Request().GetAsync();
}
答案 0 :(得分:0)
关于此问题,您需要在Controller和html中指定代码。有一个示例如下。
public async Task<ActionResult> Test()
{
string clientId = "";
string secrect = "";
string resrouce = "https://graph.microsoft.com";
string authority = "https://login.microsoftonline.com/tenanId";
AuthenticationContext authContext = new AuthenticationContext(authority);
ClientCredential clientCredential = new ClientCredential(clientId, secrect);
AuthenticationResult authResult = await authContext.AcquireTokenAsync(resrouce, clientCredential);
var token = authResult.AccessToken;
var graphServiceClient = new GraphServiceClient(new DelegateAuthenticationProvider((requestMessage) => {
requestMessage
.Headers
.Authorization = new AuthenticationHeaderValue("bearer", token);
return Task.FromResult(0);
}));
// var events = await graphServiceClient.Me.Events.Request().GetAsync();
var users = await graphServiceClient.Users.Request().GetAsync();
IList<User> userlist = users.CurrentPage;
return View(userlist);
}
HTML:
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Sign-In with Microsoft Sample</title>
<link href="@Url.Content("~/Content/bootstrap.min.css")" rel="stylesheet" type="text/css" />
</head>
<body style="padding:50px">
<!--show the message your need-->
<table class="table">
<thead>
<tr>
<th scope="col">userPrincipalName</th>
<th scope="col">Mail</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>@item.UserPrincipalName</td>
<td>@item.Mail</td>
</tr>
}
</tbody>
</table>
</body>
</html>
有关更多详细信息,请参阅https://github.com/microsoftgraph/msgraph-training-aspnetmvcapp。
答案 1 :(得分:0)
对于在控制台应用程序上进行测试,您可以在下面替换此代码:
static async Task AccessMicrosoftUserData()
{
string clientId = "Your application Application Id";
string secrect = "Your application secret Id";
string resrouce = "https://graph.microsoft.com";
string authority = "https://login.microsoftonline.com/YourTenantId";
// For example
// string authority = "https://login.microsoftonline.com/b6603c7be-a866-4666-ad87-e6921e61f999";
AuthenticationContext authContext = new AuthenticationContext(authority);
//Checking application authenticity
ClientCredential clientCredential = new ClientCredential(clientId, secrect);
AuthenticationResult authResult = await authContext.AcquireTokenAsync(resrouce, clientCredential);
//Generating Token with your credentails
var accessToken = authResult.AccessToken;
var graphServiceClient = new GraphServiceClient(new DelegateAuthenticationProvider((requestMessage) => {
requestMessage
.Headers
.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
return Task.FromResult(0);
}));
//You may encounter request denial here if you don't have resource access
Privilege
//To avoid this see the screen shot below.
var users = await graphServiceClient.Users.Request().GetAsync();
}
现在在主方法中调用,就像这样:
static void Main(string[] args)
{
AccessMicrosoftUserData().Wait();
}
网络示例
var request = new HttpRequestMessage(HttpMethod.Post, "http://server.com/token");
request.Content = new FormUrlEncodedContent(new Dictionary<string, string> {
{ "client_id", "your client_id" },
{ "client_secret", "your client_secret" },
{ "grant_type", "client_credentials" }
});
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
var payload = JObject.Parse(await response.Content.ReadAsStringAsync());
var token = payload.Value<string>("access_token");
注意:在上面的代码中,
http://server.com/token
应该是您的令牌终结点,例如https://login.microsoftonline.com/YourTenantID/oauth2/v2.0/token
要了解实现的复杂性,可以检查here。如果您需要有关草稿开发的更多想法,也可以参考this
注意:如果您没有资源访问权限,则可能会遇到请求被拒绝的情况 为避免这种情况,请参见以下屏幕截图: