How to acquire a user based token from Azure Graph API

前端 未结 2 754
执笔经年
执笔经年 2021-01-29 05:21

I have an Azure Active Directory and in my Web Api I have a piece of code that I can get a token from Azure Graph Api using the Application that I have registered with Azure and

2条回答
  •  不知归路
    2021-01-29 05:52

    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 ...

提交回复
热议问题