public async Task <Result> AuthorizePostAsync <T>(T command, string rout)
        {
            try
            {
                var token = await _currentUser.GetAuthToken();

                var modelAsJson = JsonConvert.SerializeObject(command);

                var requestMessage = new HttpRequestMessage(HttpMethod.Post, rout);

                requestMessage.Headers.Authorization
                    = new AuthenticationHeaderValue("Bearer", token);

                requestMessage.Content = new StringContent(modelAsJson);

                requestMessage.Content.Headers.ContentType
                    = new MediaTypeHeaderValue("application/json");

                var response = await _httpClient.SendAsync(requestMessage);

                return(response.DeserializeResponseMessageStatus().Result);
            }
            catch (Exception)
            {
                return(CommonResults.InternalServerError("Internal Server Error", "سرور در حال بارگذاری می باشد"));
            }
        }
Example #2
0
        public async Task <Result> RefreshTokenAsync()
        {
            try
            {
                var refreshToken = new RefreshTokenModel()
                {
                    RefreshToken = await _currentUser.GetAuthRefreshToken(),
                    Token        = await _currentUser.GetAuthToken()
                };
                var loginAsJson = JsonConvert.SerializeObject(refreshToken);

                var response = await httpClient.PostAsync("api/v1/OAuth/RefreshToken",
                                                          new StringContent(loginAsJson, Encoding.UTF8, "application/json"));

                var result = JsonConvert.DeserializeObject <AuthenticationResult>(await response.Content.ReadAsStringAsync());

                if (!response.IsSuccessStatusCode)
                {
                    return(Result.Failure(result.EnError, result.FaError));
                }

                await SetAuthentication(result.Token, result.RefreshToken);

                return(Result.Success());
            }
            catch (Exception)
            {
                return(CommonResults.InternalServerError("Internal Server Error", "سرور در حال بارگذاری می باشد"));
            }
        }
Example #3
0
        public override async Task <AuthenticationState> GetAuthenticationStateAsync()
        {
            var savedToken = await _currentUserService.GetAuthToken();

            if (string.IsNullOrWhiteSpace(savedToken))
            {
                return(new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity())));
            }

            return(new AuthenticationState(new ClaimsPrincipal(await GetPrincipleFromToken(savedToken))));
        }