Exemplo n.º 1
0
        /// <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);
        }
Exemplo n.º 2
0
        public async Task <Unit> Handle(RegisterUserTokenCommand request, CancellationToken cancellationToken)
        {
            var user = _userAccessor.GetUser();

            var dict = new Dictionary <string, string>
            {
                { "client_id", _config.CurrentValue.ClientId },
                { "redirect_uri", _config.CurrentValue.ReturnUrl },
                { "client_secret", _config.CurrentValue.ClientSecret },
                { "code", request.OAuthCode },
                { "grant_type", "authorization_code" }
            };

            _accessor.Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            var postAction = await _accessor.Client.PostAsync("https://login.microsoftonline.com/common/oauth2/v2.0/token", new FormUrlEncodedContent(dict), cancellationToken);

            var result = JsonConvert.DeserializeObject <CloudLoginModel>(await postAction.Content.ReadAsStringAsync());
            await _userTokenDataService.SetUpUserToken(result.AccessToken, result.RefreshToken, Guid.Parse(user.FindFirst(ClaimTypes.NameIdentifier).Value),
                                                       ExternalProviders.OneDrive);

            return(Unit.Value);
        }