using Microsoft.IdentityModel.Clients.ActiveDirectory; static async TaskGetAccessToken(string refreshToken) { var authenticationContext = new AuthenticationContext("https://login.microsoftonline.com/tenantId"); var result = await authenticationContext.AcquireTokenByRefreshTokenAsync(refreshToken, clientId, redirectUri); return result.AccessToken; }
using Microsoft.Identity.Web; using Microsoft.Identity.Web.TokenCacheProviders.InMemory; static async TaskThis example shows how to use the AcquireTokenByRefreshToken method in the Microsoft.Identity.Web package library to get a new access token in a web application. The method is invoked on a confidential client application instance that is created by using the ConfidentialClientApplicationBuilder class. The application is authenticated by using the client ID, client secret, and tenant ID. A token cache provider is also used to store the access token and refresh token.GetAccessToken(string refreshToken) { var tokenCacheProvider = new MsalMemoryTokenCacheProvider(); var confidentialClientApplication = ConfidentialClientApplicationBuilder .Create(clientId) .WithRedirectUri(redirectUri) .WithClientSecret(clientSecret) .WithAuthority($"https://login.microsoftonline.com/{tenantId}") .Build(); var authenticationResult = await confidentialClientApplication.AcquireTokenByRefreshToken(scopes, refreshToken) .ExecuteAsync(); return authenticationResult.AccessToken; }