public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
            var user = _userService.Authenticate(context.UserName, context.Password);

            if (user == null)
            {
                context.SetError("invalid_grant", "Usuário ou senha inválidos");
                return;
            }

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

            identity.AddClaim(new Claim(ClaimTypes.Name, user.Email));
            foreach (var item in _userService.GetAllRoles(user.Id))
            {
                identity.AddClaim(new Claim(ClaimTypes.Role, item));
            }
            GenericPrincipal principal = new GenericPrincipal(identity, new string[] {});

            Thread.CurrentPrincipal = principal;


            context.Validated(identity);
        }
Esempio n. 2
0
        public Task <HttpResponseMessage> Get()
        {
            var user  = _serviceUser.GetOneByEmailIncludePerson(User.Identity.Name);
            var roles = _serviceUser.GetAllRoles(user.Id);

            return(CreateResponse(HttpStatusCode.OK, new UserRoleViewModel(user, roles)));
        }
        public Task <HttpResponseMessage> Get()
        {
            var users = _service.GetAllIncludeDetails();
            List <UserRoleViewModel> listUsersRoles = new List <UserRoleViewModel>();

            foreach (var user in users)
            {
                var roles = _service.GetAllRoles(user.Id);
                listUsersRoles.Add(new UserRoleViewModel(user, roles));
            }
            return(CreateResponse(HttpStatusCode.OK, listUsersRoles));
        }