您好:我正在尝试将项目转换为使用较新的ActiveDirectoryClient。我已经使用Graph 1客户端和较旧的Azure Portal做了几次,但我遇到了“权限不足”的错误。
几个问题:
谢谢!
public IUser FindUser(string username)
{
var client = GetClient();
if (client != null)
{
return client.Users.Where(x => x.UserPrincipalName == username).ExecuteSingleAsync().Result;
}
else
{
return null;
}
}
private static ActiveDirectoryClient GetClient()
{
try
{
Uri servicePointUri = new Uri(_graphName);
Uri serviceRoot = new Uri(servicePointUri, _tenantName);
var client = new ActiveDirectoryClient(serviceRoot, async () => { return await GetToken(servicePointUri); });
return client;
}
catch (Exception ex) {
return null;
}
}
private static async Task<string> GetToken(Uri servicePointUri)
{
var authenticationContext = new AuthenticationContext(_authString, false);
var credentials = new ClientCredential(_clientId, _clientSecret);
var result = authenticationContext.AcquireToken(servicePointUri.ToString(), credentials);
return result.AccessToken;
}
答案 0 :(得分:0)
我建议使用Microsoft Graph。您可以在这里找到一个控制台应用程序(而不是MVC ASP.Net Web应用程序):https://github.com/microsoftgraph/console-csharp-snippets-sample
如果坚持继续使用Azure AD Graph,那么还有一个更合适的控制台应用示例。它可以在这里找到:https://github.com/Azure-Samples/active-directory-dotnet-graphapi-console。但同样 - 我强烈建议使用Microsoft Graph。
希望这有帮助,