コード例 #1
0
        public void Can_add_a_user_to_a_role()
        {
            var role = new Role("Test Role");
            var user = new User(userName, password, email, passwordQuestion, passwordAnswer);

            user.AddToRole(role);

            Assert.True(user.Roles.Contains(role));
        }
コード例 #2
0
        public void Can_remove_a_user_from_a_role()
        {
            var role = new Role("Test Role");
            var user = new User(userName, password, email, passwordQuestion, passwordAnswer);

            user.AddToRole(role);

            Assert.True(user.Roles.Contains(role));

            user.RemoveFromRole(role);

            Assert.False(user.Roles.Contains(role));
        }
コード例 #3
0
        //Fn to create a Membership user from a Entities.Users class
        private MembershipUser GetMembershipUserFromUser(User user)
        {
            var membershipUser = new MembershipUser(Name,
                user.Username,
                user.Id,
                user.Email,
                user.PasswordQuestion,
                user.Comment,
                user.IsApproved,
                user.IsLockedOut,
                user.CreationDate,
                user.LastLoginDate,
                user.LastActivityDate,
                user.LastPasswordChangedDate,
                user.LastLockedOutDate);

            return membershipUser;
        }
コード例 #4
0
        // Create a new Membership user
        public override MembershipUser CreateUser(string username,
            string password,
            string email,
            string passwordQuestion,
            string passwordAnswer,
            bool isApproved,
            object providerUserKey,
            out MembershipCreateStatus status)
        {
            var args = new ValidatePasswordEventArgs(username, password, true);

            OnValidatingPassword(args);
            if (args.Cancel)
            {
                status = MembershipCreateStatus.InvalidPassword;
                return null;
            }

            if (RequiresUniqueEmail && GetUserNameByEmail(email) != "")
            {
                status = MembershipCreateStatus.DuplicateEmail;
                return null;
            }

            var user = GetUser(username, false);

            if (user == null)
            {
                var createDate = DateTime.UtcNow;

                //provider user key in our case is auto int
                var newUser = new User(username, EncodePassword(password), email, passwordQuestion, passwordAnswer)
                {
                    IsApproved = isApproved,
                    Comment = "",
                    CreationDate = createDate,
                    LastPasswordChangedDate = createDate,
                    LastActivityDate = createDate,
                    ApplicationName = _applicationName,
                    IsLockedOut = false,
                    LastLockedOutDate = createDate,
                    FailedPasswordAttemptCount = 0,
                    FailedPasswordAttemptWindowStart = createDate,
                    FailedPasswordAnswerAttemptCount = 0,
                    FailedPasswordAnswerAttemptWindowStart = createDate
                };

                try
                {
                    UserRepository.Create(newUser);
                    UserRepository.SaveChanges();
                    status = MembershipCreateStatus.Success;
                }
                catch (Exception e)
                {
                    status = MembershipCreateStatus.ProviderError;
                    if (WriteExceptionsToEventLog)
                        WriteToEventLog(e, "CreateUser");
                }

                //retrive and return user by user name
                return GetUser(username, false);
            }

            status = MembershipCreateStatus.DuplicateUserName;
            return null;
        }