Esempio n. 1
0
        public override async Task AddToRoleAsync(ApplicationUser <TKey> user, string normalizedRoleName, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            user.ThrowIfNull(nameof(user));

            if (string.IsNullOrEmpty(normalizedRoleName))
            {
                throw new ArgumentException($"Parameter {nameof(normalizedRoleName)} cannot be null or empty.");
            }

            var role = await FindRoleAsync(normalizedRoleName, cancellationToken);

            if (role == null)
            {
                throw new InvalidOperationException($"Role '{normalizedRoleName}' was not found.");
            }

            var userRoles = (await UserRolesTable.GetRolesAsync(user.Id))?.Select(x => new IdentityUserRole <TKey> {
                UserId = user.Id,
                RoleId = x.Id
            }).ToList();

            UserRoles = userRoles;
            UserRoles.Add(CreateUserRole(user, role));
        }
Esempio n. 2
0
        protected override Task <IdentityUserRole <TKey> > FindUserRoleAsync(TKey userId, TKey roleId, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            return(UserRolesTable.FindUsersRoleAsync(userId, roleId));
        }
Esempio n. 3
0
        public override async Task RemoveFromRoleAsync(ApplicationUser <TKey> user, string normalizedRoleName, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            user.ThrowIfNull(nameof(user));
            if (string.IsNullOrEmpty(normalizedRoleName))
            {
                throw new ArgumentException(nameof(normalizedRoleName));
            }
            var roleEntity = await FindRoleAsync(normalizedRoleName, cancellationToken);

            if (roleEntity != null)
            {
                var userRoles = (await UserRolesTable.GetRolesAsync(user.Id))?.Select(x => new IdentityUserRole <TKey> {
                    UserId = user.Id,
                    RoleId = x.Id
                }).ToList();
                UserRoles = userRoles;
                var userRole = await FindUserRoleAsync(user.Id, roleEntity.Id, cancellationToken);

                if (userRole != null)
                {
                    UserRoles.Remove(userRole);
                }
            }
        }
Esempio n. 4
0
        /// <inheritdoc/>
        protected override async Task <TUserRole> FindUserRoleAsync(TKey userId, TKey roleId, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            var userRole = await UserRolesTable.FindUserRoleAsync(userId, roleId);

            return(userRole);
        }
Esempio n. 5
0
        public override async Task <IList <string> > GetRolesAsync(ApplicationUser <TKey> user, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            user.ThrowIfNull(nameof(user));
            var userLogins = await UserRolesTable.GetRolesAsync(user.Id);

            return(userLogins.Select(x => x.Name).ToList());
        }
Esempio n. 6
0
 public UserStore(IUsersContext context)
 {
     _usersTable       = new UsersTable(context);
     _usersRolesTable  = new UserRolesTable(context);
     _rolesTable       = new RolesTable(context);
     _usersClaimsTable = new UserClaimsTable(context);
     _usersLoginsTable = new UserLoginsTable(context);
     _userTokensTable  = new UserTokensTable(context);
 }
 public UserStore(IDbConnectionFactory dbConnectionFactory)
 {
     DbConnectionFactory = dbConnectionFactory;
     UserTable           = new UserTable <TUser>(dbConnectionFactory);
     RoleTable           = new RoleTable(dbConnectionFactory);
     UserRolesTable      = new UserRolesTable(dbConnectionFactory);
     UserClaimsTable     = new UserClaimsTable(dbConnectionFactory);
     UserLoginsTable     = new UserLoginsTable(dbConnectionFactory);
 }
Esempio n. 8
0
 public UserStore(IDatabaseConnectionFactory databaseConnectionFactory)
 {
     _usersTable       = new UsersTable(databaseConnectionFactory);
     _usersRolesTable  = new UserRolesTable(databaseConnectionFactory);
     _rolesTable       = new RolesTable(databaseConnectionFactory);
     _usersClaimsTable = new UserClaimsTable(databaseConnectionFactory);
     _usersLoginsTable = new UserLoginsTable(databaseConnectionFactory);
     _userTokensTable  = new UserTokensTable(databaseConnectionFactory);
 }
        public UserStore(SqlServerDatabase database)
        {
            Database = database;

            _userTable       = new UserTable <T>(database);
            _userLoginsTable = new UserLoginsTable <T>(database);
            _roleTable       = new RoleTable <ApplicationRole>(database);
            _userRolesTable  = new UserRolesTable <T>(database);
            _userClaimsTable = new UserClaimsTable(database);
        }
        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 AddToRoleAsync(TUser user, string roleName)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            IRole <int> role = await RoleTable.GetRoleByName(roleName);

            if (role != null)
            {
                await UserRolesTable.Insert(user, role.Id);
            }
        }
        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));
        }
Esempio n. 13
0
 public UserStore(
     UsersTable <TKey> usersTable,
     UserClaimsTable <TKey> userClaimsTable,
     UserRolesTable <TKey> userRolesTable,
     UserLoginsTable <TKey> userLoginsTable,
     UserTokensTable <TKey> userTokensTable,
     RolesTable <TKey> rolesTable,
     IdentityErrorDescriber describer
     ) : base(describer)
 {
     UsersTable      = usersTable ?? throw new ArgumentException(nameof(usersTable));
     UserClaimsTable = userClaimsTable ?? throw new ArgumentException(nameof(userClaimsTable));
     UserRolesTable  = userRolesTable ?? throw new ArgumentException(nameof(userRolesTable));
     UserLoginsTable = userLoginsTable ?? throw new ArgumentException(nameof(userLoginsTable));
     UserTokensTable = userTokensTable ?? throw new ArgumentException(nameof(userTokensTable));
     RolesTable      = rolesTable ?? throw new ArgumentException(nameof(rolesTable));
 }
Esempio n. 14
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));
            }
        }
Esempio n. 15
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                //var roleAssign = UserManager.AddToRole(user.Id, model.RoleName);
                if (result.Succeeded)
                {
                    UserRolesTable rt = new UserRolesTable(new DbManager(""));
                    rt.Insert(user, model.RoleId);

                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    //return RedirectToAction("Index", "Home");
                    if (model.RoleId == 1)
                    {
                        return(RedirectToAction("Index", "Services"));
                    }
                    else if (model.RoleId == 2)
                    {
                        return(RedirectToAction("Index", "Subscriptions"));
                    }
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Esempio n. 16
0
        public void ClearUserRoles(string userId)
        {
            UserRolesTable userRolesTable = new UserRolesTable(new ApplicationDbContext("DefaultConnection"));

            userRolesTable.Delete(userId);
        }