示例#1
0
        public static async Task <HttpResponseMessage> GetAuthAsync(this HttpClient httpClient, string requestUri, IJSRuntime jsRuntime)
        {
            string token = await jsRuntime.GetUserTokenAsync();

            //set the token for the authentication
            httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {token}");
            HttpResponseMessage response = await httpClient.GetAsync(requestUri);

            return(response);
        }
示例#2
0
        public static async Task <HttpResponseMessage> PostAuthAsync(this HttpClient httpClient, IJSRuntime jsRuntime, string requestUri, MultipartFormDataContent value)
        {
            string token = await jsRuntime.GetUserTokenAsync();

            //set the token for the authentication
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
            HttpResponseMessage response = await httpClient.PostAsync(requestUri, value);

            return(response);
        }
示例#3
0
        public static async Task <TValue> GetAuthAsync <TValue>(this HttpClient httpClient, IJSRuntime jsRuntime, string requestUri)
        {
            string token = await jsRuntime.GetUserTokenAsync();

            //set the token for the authentication
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
            TValue response = await httpClient.GetFromJsonAsync <TValue>(requestUri);

            return(response);
        }
示例#4
0
        public static async Task <object> GetAuthAsync(this HttpClient httpClient, IJSRuntime jsRuntime, string requestUri)
        {
            string token = await jsRuntime.GetUserTokenAsync();

            //set the token for the authentication
            httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {token}");
            object response = await httpClient.GetFromJsonAsync <object>(requestUri);

            return(response);
        }
示例#5
0
        public static async Task <HttpResponseMessage> SendAuthAsync(this HttpClient httpClient, IJSRuntime jsRuntime, HttpMethod method, string requestUri)
        {
            string token = await jsRuntime.GetUserTokenAsync();

            //check is the user are authenticated
            HttpRequestMessage requestMessage = new HttpRequestMessage(method, requestUri);

            //set the token for the authentication
            requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
            HttpResponseMessage response = await httpClient.SendAsync(requestMessage);

            return(response);
        }
示例#6
0
        /// <summary>
        /// Get the claims about the active user and return the user model
        /// Authentication used jwt authentication
        /// </summary>
        /// <typeparam name="TUser"></typeparam>
        /// <param name="jsRuntime"></param>
        /// <returns></returns>
        public static async Task <TUser> GetUserAsync <TUser>(IJSRuntime jsRuntime) where TUser : new ()
        {
            string data = await jsRuntime.GetUserDataAsync();

            if (string.IsNullOrEmpty(data))
            {
                return(new TUser());
            }
            else
            {
                string token = await jsRuntime.GetUserTokenAsync();

                string        key        = token.Split('.')[2];
                Cipher.Secret e          = new Cipher.Secret(key);
                string        jsonString = await e.DecriptAsync(data);

                return(JsonSerializer.Deserialize <TUser>(jsonString));
            }
        }
示例#7
0
        /// <summary>
        /// Login request from a model user send
        /// Authentication used jwt authentication with token
        /// </summary>
        /// <param name="jsRuntime"></param>
        /// <returns></returns>
        public static async Task <bool> SessionExpiredAsync(IJSRuntime jsRuntime)
        {
            bool result;

            try
            {
                string token = await jsRuntime.GetUserTokenAsync();

                string         payload = Cipher.Hash.Base64.Base64UrlDecode(token.Split('.')[1]);
                JsonElement    json    = JsonSerializer.Deserialize <JsonElement>(payload);
                DateTimeOffset expired = DateTimeOffset.FromUnixTimeSeconds(json.GetProperty("exp").GetInt64());
                result = expired <= DateTime.Now;
            }
            catch (Exception ex)
            {
                string err = ex.Message;
                result = true;
            }
            return(result);
        }