private async Task <ResponseModel> SendRequest(StringContent content, RequestVerbs verb, string endpoint) { var token = _identity.GetTokens().Token; var response = await _userWebApiHelper.SendRequest(endpoint, verb, content, token); var result = await _userWebApiHelper.GetResultFromResponse <string>(response, "message"); if (!result.IsSuccess && result.Message.Equals(UserWebApiHelper.TokenExpiredHeaderKey, StringComparison.OrdinalIgnoreCase)) { await RefreshToken(); await SendRequest(content, verb, endpoint); } return(result); }
//identity's creating after sign in public async Task <ResponseModel> CreateIdentity(LoginViewModel userCredentials) { var content = new StringContent(JsonConvert.SerializeObject(userCredentials), Encoding.UTF8, "application/json"); var response = await _requestHelper.SendRequest("api/authenticate/token", RequestVerbs.POST, content); try { var getResult = await _requestHelper.GetResultFromResponse <TokenModel>(response, "token"); if (!getResult.IsSuccess) { return(getResult); } var tokens = getResult.Object as TokenModel; if (tokens is null) { throw new SecurityTokenException("Invalid token model"); } return(CreateIdentityFromToken(tokens.Token, tokens.RefreshToken)); } catch (JsonSerializationException exception) { return(new ResponseModel { Message = exception.Message, IsSuccess = false }); } }