public async Task <IList <string> > GetRolesAsync(TUser user)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            IEnumerable <string> roleNames = await UserRolesTable.FindByUserId(user.Id);

            return(roleNames.ToList());
        }
        public async Task <bool> IsInRoleAsync(TUser user, string role)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            if (string.IsNullOrEmpty(role))
            {
                throw new ArgumentNullException("role");
            }

            var roleNames = await UserRolesTable.FindByUserId(user.Id);

            return(roleNames.Contains(role));
        }
예제 #3
0
        public async Task <ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            // This doesn't count login failures towards account lockout
            // To enable password failures to trigger account lockout, change to shouldLockout: true
            var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout : false);

            switch (result)
            {
            case SignInStatus.Success:
                UserRolesTable rt = new UserRolesTable(new DbManager(""));
                UserTable <ApplicationUser> ut = new UserTable <ApplicationUser>(new DbManager(""));
                int           memberId         = ut.GetmemberId(model.Email);
                List <string> roleOfUser       = rt.FindByUserId(memberId);
                if (roleOfUser.Contains("Admin"))
                {
                    return(RedirectToAction("Index", "Services"));
                }
                else if (roleOfUser.Contains("Member"))
                {
                    return(RedirectToAction("Index", "Subscriptions"));
                }
                return(RedirectToLocal(returnUrl));

            case SignInStatus.LockedOut:
                return(View("Lockout"));

            case SignInStatus.RequiresVerification:
                return(RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe }));

            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "Invalid login attempt.");
                return(View(model));
            }
        }