How to acquire a user based token from Azure Graph API

点点圈 提交于 2019-12-02 10:40:59

AcquireToken has another overload that takes in a UserCredential object and U assume you could use that (You will need the TenantId of the Active Directory that the users you need to acquire the tokens for)

Your function will look something like this: (Please fill in the _variables with your own application information)

public static string AcquireTokenWithoutUserCredentials(string userName, string password)
{
    var authContext = new AuthenticationContext(string.Format(_authority, _userTokenTenantId));
    var userCreds = new UserCredential(userName, password);
    var result = authContext.AcquireToken(_resourceId, _userTokenClientId, userCreds);
    return result.AccessToken;
}

Looking at your code, looks like you have a multi tenant scenario, in which you use "common" as TenatName, which I'm not sure how/if it will work using the code I pasted here but give it a try ...

A web API can access another web API (in this case, the Graph) as the current user by obtaining a new token via the onbehalfof flow. See https://github.com/Azure-Samples/active-directory-dotnet-webapi-onbehalfof for an example. The direct use of username and password is not recommended, credentials should never be collected outside of Azure AD own pages, and in this specific case it would not work (or will stop working soon)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!