Beispiel #1
0
        protected async override Task <AuthenticateResult> HandleAuthenticateAsync()
        {
            var token = await _tokenService.CheckToken(Request
                                                       .Headers["Authorization"]
                                                       .FirstOrDefault()?
                                                       .Split(" ")
                                                       .Last() ?? string.Empty);

            if (!token.IsValid)
            {
                return(AuthenticateResult.Fail($"{token.Error} {token.ErrorDescription}."));
            }

            if (string.IsNullOrEmpty(token.UserName))
            {
                return(AuthenticateResult.Fail("Not a user access."));
            }

            var context = await _userContextService.GetInfo(token.UserName);

            var claims = context
                         .Roles
                         .Concat(context.TechnicalRoles)
                         .Select(role => new Claim(ClaimTypes.Role, role))
                         .Append(new Claim(ClaimTypes.Name, context.Username))
                         .Append(new Claim(ClaimTypes.GivenName, context.FirstName))
                         .Append(new Claim(ClaimTypes.Surname, context.LastName));

            var identity   = new ClaimsIdentity(claims, Options.AuthenticationType);
            var identities = new List <ClaimsIdentity> {
                identity
            };
            var principal = new ClaimsPrincipal(identities);
            var ticket    = new AuthenticationTicket(principal, Options.Scheme);

            return(AuthenticateResult.Success(ticket));
        }