예제 #1
0
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            try
            {
                IServiceUsuario serviceUsuario = _container.Resolve <IServiceUsuario>();
                var             request        = new UsuarioLoginRequest();
                request.Email = context.UserName;
                request.Senha = context.Password;
                UsuarioLoginResponse response = serviceUsuario.Autenticar(request);

                if ((serviceUsuario.IsInvalid()) || (response == null))
                {
                    context.SetError("invalid_grant", "Usuário não encontrado.");
                    serviceUsuario.ClearNotifications();
                    return;
                }

                var identity = new ClaimsIdentity(context.Options.AuthenticationType);
                identity.AddClaim(new Claim(ClaimTypes.Name, response.Nome));
                identity.AddClaim(new Claim(ClaimTypes.Role, response.Perfil));

                var roles = new List <string>();
                roles.Add(response.Perfil);

                var principal = new GenericPrincipal(identity, roles.ToArray());
                Thread.CurrentPrincipal = principal;
                context.Validated(identity);
            }
            catch (Exception ex)
            {
                context.SetError("invalid_grant", ex.Message);
                return;
            }
        }
예제 #2
0
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            try
            {
                IServiceUsuario serviceUsuario = _container.Resolve <IServiceUsuario>();


                AutenticarUsuarioRequest request = new AutenticarUsuarioRequest();
                request.Email = context.UserName;
                request.Senha = context.Password;

                AutenticarUsuarioResponse response = serviceUsuario.AutenticarUsuario(request);



                if (serviceUsuario.IsInvalid())
                {
                    if (response == null)
                    {
                        context.SetError("invalid_grant", "Preencha um e-mail válido e uma senha com pelo menos 6 caracteres.");
                        return;
                    }
                }

                serviceUsuario.ClearNotifications();

                if (response == null)
                {
                    context.SetError("invalid_grant", "Usuario não encontrado!");
                    return;
                }

                var identity = new ClaimsIdentity(context.Options.AuthenticationType);

                //Definindo as Claims
                identity.AddClaim(new Claim("Usuario", JsonConvert.SerializeObject(response)));

                var principal = new GenericPrincipal(identity, new string[] { });

                Thread.CurrentPrincipal = principal;

                context.Validated(identity);
            }
            catch (Exception ex)
            {
                context.SetError("invalid_grant", ex.Message);
                return;
            }
        }