public async Task <Unit> Handle(RefreshCurrentUserTokenCommand request, CancellationToken cancellationToken) { var userId = Guid.Parse(_userAccessor.GetUser().FindFirst(ClaimTypes.NameIdentifier).Value); var accessToken = await _tokenDataService.GetUserAccessToken(userId, request.ExternalProvider); var userRefreshToken = await _tokenDataService.GetUserRefreshToken(userId, request.ExternalProvider); await _authorizationService.VerifyUser(accessToken, userRefreshToken , userId, request.ExternalProvider, _config.CurrentValue.ClientId, _config.CurrentValue.ReturnUrl, _config.CurrentValue.ClientSecret); return(Unit.Value); }
/// <summary> /// Refresh user token /// By providing the refresh token from database to the Graph API /// </summary> /// <param name="userId"></param> /// <param name="provider"></param> /// <param name="clientId"></param> /// <param name="redirectUri"></param> /// <param name="clientSecret"></param> private async Task RefreshUserToken(Guid userId, ExternalProviders provider, string clientId, string redirectUri, string clientSecret) { var dict = new Dictionary <string, string> { { "client_id", clientId }, { "redirect_uri", redirectUri }, { "client_secret", clientSecret }, { "refresh_token", await _dataService.GetUserRefreshToken(userId, provider) }, { "grant_type", "refresh_token" } }; var client = new HttpClient(); dict.TryGetValue("refresh_token", out var refreshToken); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var postAction = await client.PostAsync("https://login.microsoftonline.com/common/oauth2/v2.0/token", new FormUrlEncodedContent(dict)); var result = JsonConvert.DeserializeObject <CloudLoginModel>(await postAction.Content.ReadAsStringAsync()); await _dataService.SetUpUserToken(result.AccessToken, refreshToken, userId, provider); }