Azure 如何获得用户';s来自MS Graph API的基本详细信息
我正在尝试使用MS Graph API获取用户的全名。代码未使用委派权限Azure 如何获得用户';s来自MS Graph API的基本详细信息,azure,azure-active-directory,microsoft-graph-api,adal,Azure,Azure Active Directory,Microsoft Graph Api,Adal,我正在尝试使用MS Graph API获取用户的全名。代码未使用委派权限User.ReadBasic.All,而使用User.Read.All的应用权限 代码是: public static async Task<string> GetAccessToken() { string authorityUri = $"https://login.microsoftonline.com/{tenantid}"; AuthenticationConte
User.ReadBasic.All
,而使用User.Read.All的应用权限
代码是:
public static async Task<string> GetAccessToken()
{
string authorityUri = $"https://login.microsoftonline.com/{tenantid}";
AuthenticationContext authContext = new AuthenticationContext(authorityUri);
string resourceUrl = "https://graph.microsoft.com";
ClientCredential creds = new ClientCredential(ConfigHelper.ClientId, ConfigHelper.AppKey);
AuthenticationResult authResult = await authContext.AcquireTokenAsync(resourceUrl, creds);
return authResult.AccessToken;
}
public static async Task<GraphServiceClient> GetGraphClient()
{
GraphServiceClient graphServiceClient = new GraphServiceClient(new DelegateAuthenticationProvider(
async (requestMessage) =>
{
string accessToken = await GetAccessToken();
if (!string.IsNullOrEmpty(accessToken))
{
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
}
}));
return graphServiceClient;
}
公共静态异步任务GetAccessToken()
{
字符串authorityUri=$”https://login.microsoftonline.com/{tenantid}”;
AuthenticationContext authContext=新的AuthenticationContext(authorityUri);
字符串resourceUrl=”https://graph.microsoft.com";
ClientCredential creds=新的ClientCredential(ConfigHelper.ClientId、ConfigHelper.AppKey);
AuthenticationResult authResult=等待authContext.AcquireTokenAsync(resourceUrl,creds);
返回authResult.AccessToken;
}
公共静态异步任务GetGraphClient()
{
GraphServiceClient GraphServiceClient=新GraphServiceClient(新DelegateAuthenticationProvider(
异步(请求消息)=>
{
字符串accessToken=等待GetAccessToken();
如果(!string.IsNullOrEmpty(accessToken))
{
requestMessage.Headers.Authorization=新的AuthenticationHeaderValue(“承载者”,accessToken);
}
}));
返回graphServiceClient;
}
错误是
Microsoft.Graph.ServiceException:代码:授权\u请求被拒绝
消息:权限不足,无法完成操作。大宗报价
我不知道为什么会这样
编辑:
private static async Task<string> GetAccessToken()
{
string signedInUserID = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
string tenantID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value;
string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
string resourceUrl = "https://graph.microsoft.com";
//// get a token for the Graph without triggering any user interaction (from the cache, via multi-resource refresh token, etc)
ClientCredential clientcred = new ClientCredential(ConfigHelper.ClientId, ConfigHelper.AppKey);
//// initialize AuthenticationContext with the token cache of the currently signed in user, as kept in the app's database
AuthenticationContext authenticationContext = new AuthenticationContext($"https://login.microsoftonline.com/{tenantid}") ;
AuthenticationResult authenticationResult = await authenticationContext.AcquireTokenSilentAsync(resourceUrl, clientcred, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));
return authenticationResult.AccessToken;
}
private静态异步任务GetAccessToken()
{
字符串signedInUserID=ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
字符串tenantID=ClaimsPrincipal.Current.FindFirst(“http://schemas.microsoft.com/identity/claims/tenantid1.价值;
字符串userObjectID=ClaimsPrincipal.Current.FindFirst(“http://schemas.microsoft.com/identity/claims/objectidentifier1.价值;
字符串resourceUrl=”https://graph.microsoft.com";
////在不触发任何用户交互的情况下获取图形的令牌(从缓存,通过多资源刷新令牌等)
ClientCredential clientcred=新的ClientCredential(ConfigHelper.ClientId,ConfigHelper.AppKey);
////使用保存在应用程序数据库中的当前登录用户的令牌缓存初始化AuthenticationContext
AuthenticationContext AuthenticationContext=新的AuthenticationContext($)https://login.microsoftonline.com/{tenantid}”);
AuthenticationResult AuthenticationResult=等待authenticationContext.AcquireTokenSilentAsync(resourceUrl、clientcred、新用户标识符(userObjectID、UserIdentifierType.UniqueId));
返回authenticationResult.AccessToken;
}
好吧,您正在使用客户端凭据授予流,期望将委托权限应用于没有用户的场景
您获得的令牌是用于应用程序的,它完全充当自身,这意味着仅应用程序权限。仅当存在用户上下文时,才会应用委派权限
您的选择:
- 当您以这种方式获得令牌时,您也会获得一个刷新令牌,您可以随时使用该令牌为该用户获取一个新的访问令牌(+一个新的刷新令牌)
- 尽管某些情况(如用户重置密码)会导致刷新令牌被撤销
- 当您以这种方式获得令牌时,您也会获得一个刷新令牌,您可以随时使用该令牌为该用户获取一个新的访问令牌(+一个新的刷新令牌)
- 尽管某些情况(如用户重置密码)会导致刷新令牌被撤销
有道理,我已经对函数进行了一些编辑,你认为现在应该可以了吗?应该可以了,如果你以前用授权代码流等获得了令牌,并且ADAL已经缓存了它。非常感谢。你的回答很有帮助,澄清了一些基本概念。谢谢。很高兴听到:)有意义,我对函数做了一些编辑,你认为现在应该可以了吗?应该可以了,如果你之前已经获得了一个令牌,比如授权代码流,并且ADAL已经缓存了它。非常感谢。你的回答很有帮助,澄清了一些基本概念。谢谢。很高兴听到:)