Esempio n. 1
0
 private void SetTokenHeader()
 {
     if (_tokenHeaderIsSet && !_credentialsKeeper.HasCredentials())
     {
         _httpClient.DefaultRequestHeaders.Remove(TOKEN_HEADER_NAME);
         _tokenHeaderIsSet = false;
     }
     else if (!_tokenHeaderIsSet && _credentialsKeeper.HasCredentials())
     {
         _httpClient.DefaultRequestHeaders.Add(TOKEN_HEADER_NAME, _credentialsKeeper.Token);
         _tokenHeaderIsSet = true;
     }
 }
        /// <summary>
        ///     Create the initial authenticationstate when loading the site for the first time
        /// </summary>
        /// <returns></returns>
        public override Task <AuthenticationState> GetAuthenticationStateAsync()
        {
            // Username is null, so return an empty claimsprinipal, so we are NOT authenticated
            if (!_credentialsKeeper.HasCredentials())
            {
                return(Task.FromResult(new AuthenticationState(new ClaimsPrincipal())));
            }

            IEnumerable <Claim> claims = ParseClaimsFromJwt(_credentialsKeeper.Token);

            //Add username to the claims
            claims.Append(new Claim(ClaimTypes.Name, _credentialsKeeper.Username));

            var user = new ClaimsPrincipal(new ClaimsIdentity(claims, "ApiWebAuth"));

            return(Task.FromResult(new AuthenticationState(user)));
        }
        /// <summary>
        ///     Create the initial authenticationstate when loading the site for the first time
        /// </summary>
        /// <returns></returns>
        public override Task <AuthenticationState> GetAuthenticationStateAsync()
        {
            // Username is null, so return an empty claimsprinipal, so we are NOT authenticated
            if (!_credentialsKeeper.HasCredentials())
            {
                return(Task.FromResult(new AuthenticationState(new ClaimsPrincipal())));
            }

            var identity = new ClaimsIdentity(new[]
            {
                new Claim(ClaimTypes.Name, _credentialsKeeper.Username)
            }, "ApiWebAuth");

            var user = new ClaimsPrincipal(identity);

            return(Task.FromResult(new AuthenticationState(user)));
        }