private void AddActiveTokenToList(TransEuTokenDTO token, ApplicationUser user) { this._tokenAccessList.Add(new TransEuAccessCredentialsDTO { Token = token, TransId = user.TransId, UserName = user.UserName }); }
private async Task <TransEuTokenDTO> GetTransEuTokenAsync() { var userName = GetUserName(); //check local cache for active access token if (this.IsAcctiveTokenOnList(userName)) { return(this.GetActiveTokenFromListByUser(userName).Token); } //get remoteToken var user = await this.GetUserWithTransEuCreds(); if (user == null) { this._contextErrorHelper.AddError("GetToken", "GetUserError"); return(null); } var httpClient = new HttpClient(); var tokenUri = new Uri("https://auth.system.trans.eu/oauth2/token"); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, tokenUri); request.Content = this.TokenFormUrlEncoded(user); var response = await httpClient.SendAsync(request); if (response.IsSuccessStatusCode) { var token = await response.Content.ReadAsStringAsync(); TransEuTokenDTO accessToken = Newtonsoft.Json.JsonConvert.DeserializeObject <TransEuTokenDTO>(token); //set token to local cache AddActiveTokenToList(accessToken, user); return(accessToken); } else { var errInfo = await response.Content.ReadAsStringAsync(); dynamic jsonErr = Newtonsoft.Json.JsonConvert.DeserializeObject(errInfo); throw new HttpRequestException(); } }