Пример #1
0
        public async Task <string> GenerateToken(Usuario usuario)
        {
            var request           = new TokenCreationRequest();
            var idServerPrincipal = IdentityServerPrincipal.Create(usuario.Id.ToString(), usuario.Login, AuthorizationConfig.GetClaims(usuario));

            request.Subject = idServerPrincipal;
            request.IncludeAllIdentityClaims = true;
            request.ValidatedRequest         = new ValidatedRequest();
            request.ValidatedRequest.Subject = request.Subject;
            request.ValidatedRequest.SetClient(AuthorizationConfig.GetClients().First());
            request.Resources = new Resources(new IdentityResource[] { }, AuthorizationConfig.GetApiResources());
            request.ValidatedRequest.Options      = _identityServerOptions;
            request.ValidatedRequest.ClientClaims = idServerPrincipal.Claims.ToArray();

            var Token = await _tokenService.CreateAccessTokenAsync(request);

            Token.Issuer = _autorizationOptions.AuthUrl;

            var TokenValue = await _tokenService.CreateSecurityTokenAsync(Token);

            return(TokenValue);
        }
Пример #2
0
        public Task ValidateAsync(ResourceOwnerPasswordValidationContext context)
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine($"Validando Login");

            var usuario = _usuarioApplication.ObterUsuarioPorLogin(context.UserName);

            if (usuario != null)
            {
                Console.WriteLine($"Usuario Obtido do banco: " + usuario.Nome);
            }

            if (usuario == null)
            {
                context.Result = new GrantValidationResult(TokenRequestErrors.UnauthorizedClient, "Login inválido.");
                return(Task.FromResult(0));
            }

            if (usuario.Senha != SHA.Encrypt(SHA.Algorithm.SHA512, context.Password))
            {
                context.Result = new GrantValidationResult(TokenRequestErrors.UnauthorizedClient, "Senha inválida.");
                return(Task.FromResult(0));
            }

            context.Result = new GrantValidationResult(subject: usuario.Id.ToString(), authenticationMethod: "custom", claims: AuthorizationConfig.GetClaims(usuario));
            Console.WriteLine($"Usuario no Claims " + usuario.Nome);
            Console.ResetColor();
            return(Task.FromResult(0));
        }