예제 #1
0
        public override async Task <AuthenticationState> GetAuthenticationStateAsync()
        {
            AuthenticationState CreateAnonymous()
            {
                var anonymousIdentity = new ClaimsIdentity();
                var anonymousPricipal = new ClaimsPrincipal(anonymousIdentity);

                return(new AuthenticationState(anonymousPricipal));
            }

            var token = await _localStorageService.GetAsync <SecurityToken>(nameof(SecurityToken));

            if (token == null)
            {
                return(CreateAnonymous());
            }

            if (string.IsNullOrEmpty(token.AccessToken) || token.ExpiredAt < DateTime.UtcNow)
            {
                return(CreateAnonymous());
            }
            // Create real user state
            var claims = new List <Claim>
            {
                new Claim(ClaimTypes.Country, "Russia"),
                new Claim(ClaimTypes.Name, token.UserName),
                new Claim(ClaimTypes.Expired, token.ExpiredAt.ToLongDateString()),
                new Claim(ClaimTypes.Role, "Administrator"),
                new Claim(ClaimTypes.Role, "Manager"),
                new Claim("Blazor", "Rulez")
            };

            var identity  = new ClaimsIdentity(claims, "Token");
            var principal = new ClaimsPrincipal(identity);

            return(new AuthenticationState(principal));
        }