/*************************************************************************
        * User related methods : create, update, unlock, delete methods.
        *************************************************************************/

        /// <summary>
        /// Creates a new user with a given set of default values
        /// </summary>
        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);
            }

            // If no user with this name already exists
            MembershipUser mu = GetUser(username, false);

            if (mu == null || mu.UserName.ToLower() == "guest")
            {
                DateTime createdDate = DateTime.UtcNow;

                //if (PasswordFormat == MembershipPasswordFormat.Hashed)
                //{
                //    string salt = GenerateSalt();
                //    password = password + salt;
                //}

                var m = new MemberInfo {
                    Username = username, Password = EncodePassword(password), Email = email
                };

                // Set the password retrieval question and answer if they are required
                if (RequiresQuestionAndAnswer)
                {
                    m.PasswordChangeKey = passwordQuestion;
                    m.ValidationKey     = EncodePassword(passwordAnswer);
                }

                m.IsValid       = isApproved;
                m.Status        = 0;
                m.ReceiveEmails = false;
                m.TimeOffset    = 0;
                m.MemberSince   = createdDate;
                m.LastVisitDate = createdDate;
                m.LastPostDate  = null;


                try
                {
                    Snitz.IDAL.IMember dal = Snitz.IDAL.Factory <IDAL.IMember> .Create("Member");

                    dal.Add(m);
                    IRoles roleDal = Factory <IRoles> .Create("Role");

                    roleDal.AddUsersToRoles(new[] { m.Username }, new[] { "all" });

                    // User creation was a success
                    status = MembershipCreateStatus.Success;

                    // Return the newly craeted user
                    return(GetUserFromMember(m));
                }
                catch
                {
                    // Something was wrong and the user was rejected
                    status = MembershipCreateStatus.UserRejected;
                }
            }
            else
            {
                // There is already a user with this name
                status = MembershipCreateStatus.DuplicateUserName;
            }

            // Something went wrong if we got this far without some sort of status or retun
            if (status != MembershipCreateStatus.UserRejected && status != MembershipCreateStatus.DuplicateUserName)
            {
                status = MembershipCreateStatus.ProviderError;
            }

            return(null);
        }
示例#2
0
        public static void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            IRoles dal = Snitz.Membership.Helpers.Factory <IRoles> .Create("Role");

            dal.AddUsersToRoles(usernames, roleNames);
        }