gpt4 book ai didi

azure - AcquireTokenAsync 函数不返回任何响应

转载 作者:行者123 更新时间:2023-12-03 04:13:37 24 4
gpt4 key购买 nike

我正在尝试使用下面提到的代码在 Web 应用程序中从 AzureAD 获取所有 office365 用户的列表。但是, authContext.AcquireTokenAsync(resrouce, clientCredential) 永远不会返回控制权。我已经尝试了下面的控制台应用程序代码,它工作得很好。但是,我很好奇为什么该代码不适用于网络,或者我必须进行哪些修改才能使代码在网络上工作。

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();
}

最佳答案

关于这个问题,您需要在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 .

关于azure - AcquireTokenAsync 函数不返回任何响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54992034/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com