Exemplo n.º 1
0
        public ActionResult CreateUser(User newUser, FormCollection formCollection)
        {
            try
            {
                _membershipProviderApplicationService.CreateUser(newUser);

                var roles = formCollection["Roles"].Replace("false", "").Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var role in roles)
                {
                    _roleProviderApplicationService.AddUserToRole(newUser.UserName, role);
                }

                return(RedirectToAction("Index"));
            }
            catch
            {
                return(RedirectToAction("CreateUser"));
            }
        }
Exemplo n.º 2
0
        public override void AddUsersToRoles(string[] usernames, string[] rolenames)
        {
            foreach (string rolename in rolenames)
            {
                if (!RoleExists(rolename))
                {
                    throw new ProviderException("Role name not found.");
                }
            }

            foreach (string username in usernames)
            {
                if (username.IndexOf(',') > 0)
                {
                    throw new ArgumentException("User names cannot contain commas.");
                }

                foreach (string rolename in rolenames)
                {
                    if (IsUserInRole(username, rolename))
                    {
                        throw new ProviderException("User is already in role.");
                    }
                }
            }

            try
            {
                foreach (string username in usernames)
                {
                    foreach (string rolename in rolenames)
                    {
                        _roleProviderApplicationService.AddUserToRole(username, rolename);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }