【发布时间】:2020-10-26 16:27:01
【问题描述】:
我正在尝试在 .net 4.5 框架中成功构建具有委托权限的图形客户端。我确实尝试了我在互联网上找到的所有方法,但所有方法都不起作用。 我在 azure ad 中注册了我的应用程序并具有委托权限,但构建它时没有任何运气。这是我尝试过的最新版本:
//private string[] _scopes = new string[] { "https://graph.microsoft.com/.default" };
//private string[] _scopes = new string[] { "https://graph.microsoft.com/User.ReadWrite.All" };
//private readonly string[] _scopes = new string[] { "User.Read" };
private readonly string[] _scopes = new string[] { "User.Read.All" };
public GraphServiceClient GetAuthenticatedGraphClient(ClaimsIdentity userIdentity) =>
new GraphServiceClient(new DelegateAuthenticationProvider(
async requestMessage =>
{
// Passing tenant ID to the sample auth provider to use as a cache key
var accessToken = await _authProvider.GetUserAccessTokenAsync();
// Append the access token to the request
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
}));
}
public async Task<string> GetUserAccessTokenAsync()
{
try
{
var result = await _app.AcquireTokenForClient(_scopes)
.ExecuteAsync();
return result.AccessToken;
}
// Unable to retrieve the access token silently.
catch (Exception ex)
{
throw new ServiceException(new Error
{
Code = GraphErrorCode.AuthenticationFailure.ToString(),
Message = "Caller needs to authenticate. Unable to retrieve the access token silently."
});
}
}
我能否获得完整的代码片段来正确构建 Microsoft 图形客户端以获得完整框架 Web 应用程序的委托权限?
【问题讨论】: