简体   繁体   中英

How to get the access token from azure using asp.net mvc

We just wanted to get the access token of azure using asp.net MVC web application.

We have to use the below line of code to get the azure access token.

 Dim authorityUri As String = "https://login.microsoftonline.com/common/oauth2/authorize"

 Dim authContext As AuthenticationContext = New AuthenticationContext(authorityUri)

 Dim TokenTask = Await authContext.AcquireTokenAsync(resource, ClientCredetial)

 Dim getToken = TokenTask.AccessToken()
 Return getToken

But we are getting a response to the open azure login page. Can you please help me with this.

Is there any way to get token from azure using asp.net MVC So please suggest Thanks in advance.

It seems that you want to implement the client credentials flow .

The authority is incorrect. It should be https://login.microsoftonline.com/{tenantId} .

Not sure why you add the C# tag but your code is VB.

So here is a C# sample for your reference.

 string authority = "https://login.microsoftonline.com/{tenantId}";
 string clientId = "{clientId}";
 string secret = "{secret}";
 string resource = "{resource}";

 var credential = new ClientCredential(clientId, secret);
 AuthenticationContext authContext = new AuthenticationContext(authority);

 var token = authContext.AcquireTokenAsync(resource, credential).Result.AccessToken;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM