public ActionResult UserLogin(LoginViewModel userModel)
        {
            var user = userService.FindwithUsernameandMail(userModel.UserName, userModel.Password);

            if (user != null)
            {
                user.roleDTO = roleService.getRole((int)user.RoleId);
                var userClaims = new List <Claim>()
                {
                    new Claim("UserDTO", BloggerConvert.BloggerJsonSerialize(user))
                };
                var userIdentity  = new ClaimsIdentity(userClaims, "User Identity");
                var userPrincipal = new ClaimsPrincipal(new[] { userIdentity });
                HttpContext.SignInAsync(userPrincipal);
                return(RedirectToAction("Index", "Home"));
            }
            return(View(user));
        }
示例#2
0
        protected override Task HandleRequirementAsync(AuthorizationHandlerContext context,
                                                       RolesAuthorizationRequirement requirement)
        {
            if (context.User == null || !context.User.Identity.IsAuthenticated)
            {
                context.Fail();
                return(Task.CompletedTask);
            }
            var validRole = false;

            if (requirement.AllowedRoles == null ||
                requirement.AllowedRoles.Any() == false)
            {
                validRole = true;
            }
            else
            {
                var claims  = context.User.Claims;
                var userDTO = BloggerConvert.BloggerJsonDeSerializeUserDTO(claims.FirstOrDefault(z => z.Type == "UserDTO").Value);
                var roles   = requirement.AllowedRoles;

                if (roles.Contains(userDTO.roleDTO.Name))
                {
                    validRole = true;
                }
            }
            if (validRole)
            {
                context.Succeed(requirement);
            }
            else
            {
                context.Fail();
            }
            return(Task.CompletedTask);
        }