Exemplo n.º 1
0
        public async Task <LoginResponseExtended> GetToken()
        {
            LoginResponseExtended token = await _sessionStorage.GetItemAsync <LoginResponseExtended>("Token");

            if (token == null)
            {
                token = await GetLocalStorageTokenAndSetSessionToken();
            }
            if (token == null)
            {
                return(null);
            }
            if ((token.ValidUntil - DateTime.Now).TotalMinutes <= 5)
            {
                await SetToken(await _sessionApi.RefreshToken(token), LoginDuration.None);

                token = await _sessionStorage.GetItemAsync <LoginResponseExtended>("Token");
            }
            return(token);
        }
        GetAuthenticationStateAsync()
        {
            var emailAddress = await _sessionStorageService.GetItemAsync <string>("emailAddress");

            System.Security.Claims.ClaimsIdentity identity;

            if (emailAddress != null)
            {
                identity = new System.Security.Claims.ClaimsIdentity(new[]
                {
                    new System.Security.Claims.Claim(System.Security.Claims.ClaimTypes.Name, emailAddress),
                }, "apiauth_type");
            }
            else
            {
                identity = new System.Security.Claims.ClaimsIdentity();
            }

            var user = new System.Security.Claims.ClaimsPrincipal(identity);

            return(await System.Threading.Tasks.Task.
                   FromResult(new Microsoft.AspNetCore.Components.Authorization.AuthenticationState(user)));
        }
        public async override Task <AuthenticationState> GetAuthenticationStateAsync()
        {
            if (context != null)
            {
                try
                {
                    username = await _sessionStorage.GetItemAsync <string>("name");
                }
                catch (Exception e)
                {
                }

                if (username != null && IsAuthorized.ContainsKey(username) && IsAuthorized[username])
                {
                    var user = await context.FindByEmailAsync(username);

                    var identity = new ClaimsIdentity(new[]
                    {
                        new Claim(ClaimTypes.Name, user.FirstName ?? user.Email),
                        new Claim(ClaimTypes.Email, user.Email),
                        new Claim(ClaimTypes.Role, user.Role ?? "freelancer"),
                        new Claim(ClaimTypes.NameIdentifier, user.Id)
                    }, "Custom authentication type");
                    var claim = new ClaimsPrincipal(identity);
                    return(await Task.FromResult(new AuthenticationState(claim)));
                }
                else
                {
                    return(new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity())));
                }
            }
            else
            {
                return(new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity())));
            }
        }