public override bool ValidateUser(string strName, string strPassword)
        {
            using (WindchimeEntities wce = new WindchimeEntities())
            {
                string pw    = SecurityManager.HashPasswordForStoringInDatabase(strPassword);
                var    users = (from User u in wce.CreatorSet.OfType <User>()
                                where u.Username == strName && u.Password == pw
                                select u);

                int num = users.Count <User>();

                if (num > 1)
                {
                    //throw new MultipleUsersException(); //doesn't exist right now
                    throw new Exception("Multiple users in system with same credentials!");
                }
                else if (num == 0)
                {
                    return(false);
                }
                else
                {
                    WindchimeSession.Current.User = users.First <User>();
                    return(true);
                }
            }
        }
        //
        // Summary:
        //     Adds a new membership user to the data source.
        //
        // Parameters:
        //   username:
        //     The user name for the new user.
        //
        //   password:
        //     The password for the new user.
        //
        //   email:
        //     The e-mail address for the new user.
        //
        //   passwordQuestion:
        //     The password question for the new user.
        //
        //   passwordAnswer:
        //     The password answer for the new user
        //
        //   isApproved:
        //     Whether or not the new user is approved to be validated.
        //
        //   providerUserKey:
        //     The unique identifier from the membership data source for the user.
        //
        //   status:
        //     A System.Web.Security.MembershipCreateStatus enumeration value indicating
        //     whether the user was created successfully.
        //
        // Returns:
        //     A System.Web.Security.MembershipUser object populated with the information
        //     for the newly created user.
        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            using (WindchimeEntities wce = new WindchimeEntities())
            {
                Regex re = new Regex(this.PasswordStrengthRegularExpression);
                User  u  = new User();
                Group g  = new Group();
                u.FirstName = "";
                u.LastName  = "";
                u.Username  = username;
                u.Password  = SecurityManager.HashPasswordForStoringInDatabase(password);
                u.IsStaff   = false;
                u.Email     = email;
                g.Name      = username;
                g.IsSpecial = false;

                if (username.Length < 6)
                {
                    status = MembershipCreateStatus.UserRejected;
                }
                else if ((from User k in wce.CreatorSet.OfType <User>()
                          where k.Username == username
                          select k).Count <User>() > 0)
                {
                    status = MembershipCreateStatus.DuplicateUserName;
                }
                else if (!re.IsMatch(password))
                {
                    status = MembershipCreateStatus.InvalidPassword;
                }
                else if (!isEmail(email))
                {
                    status = MembershipCreateStatus.InvalidEmail;
                }
                else if ((from User k in wce.CreatorSet.OfType <User>()
                          where k.Email == email
                          select k).Count <User>() > 0)
                {
                    status = MembershipCreateStatus.DuplicateEmail;
                }
                else
                {
                    status = MembershipCreateStatus.Success;
                    wce.AddToCreatorSet(u);
                    wce.AddToGroups(g);
                    g.Users.Add(u);
                    wce.SaveChanges();
                    // log in the user
                    WindchimeSession.Current.User = u;
                }
            }

            return(null);
        }
Exemple #3
0
        protected void Submit_Click(object sender, EventArgs e)
        {
            if (!Page.IsValid)
            {
                reset_data();
            }
            using (WindchimeEntities wce = new WindchimeEntities())
            {
                var user1 = (from User u in wce.CreatorSet.OfType <User>()
                             where u.Username.Equals(WindchimeSession.Current.User.Username)
                             select u);
                if (user1.Count() != 1)
                {
                    Response.Redirect("~/Login.aspx?ReturnUrl=/UserPref.aspx", false);
                    return;
                }
                User  usr      = user1.First <User>();
                Regex RegexObj = new Regex("^\\w[\\w.]*@\\w+\\.\\w[\\.\\w]*$");
                if ((!boxEmail.Text.Equals("")) && RegexObj.IsMatch(boxEmail.Text))
                {
                    usr.Email = boxEmail.Text;
                }
                if (!boxFirstName.Text.Equals(""))
                {
                    usr.FirstName = boxFirstName.Text;
                }
                if (!boxLastName.Text.Equals(""))
                {
                    usr.LastName = boxLastName.Text;
                }
                if (!boxAddr1.Text.Equals(""))
                {
                    usr.Address1 = boxAddr1.Text;
                }
                if (!boxAddr2.Text.Equals(""))
                {
                    usr.Address2 = boxAddr2.Text;
                }
                if (!boxCity.Text.Equals(""))
                {
                    usr.City = boxCity.Text;
                }
                usr.State = listState.SelectedValue;
                if (!boxZip.Text.Equals(""))
                {
                    usr.PostalCode = boxZip.Text;
                }

                WCMembershipProvider wcm = new WCMembershipProvider();
                Regex regexPassword      = new Regex(wcm.PasswordStrengthRegularExpression);
                if (boxPassword1.Text.Equals(boxPassword2.Text) && (!boxPassword1.Text.Equals("")))
                {
                    if (regexPassword.IsMatch(boxPassword1.Text))
                    {
                        usr.Password = SecurityManager.HashPasswordForStoringInDatabase(boxPassword1.Text);
                    }
                }
                wce.SaveChanges();
            }
            reset_data();
        }
        public static User CreateUser(Creator c, string username, string password, bool isStaff, bool overRide)
        {
            if (!overRide && !DoesUserHavePolicy(WindchimeSession.Current.User, Policy.CreateUser))
            {
                throw new NoPolicyException(Policy.CreateUser);
            }

            WCMembershipProvider wcm = new WCMembershipProvider();
            Regex re = new Regex(wcm.PasswordStrengthRegularExpression);
            User  u;
            Group g = new Group();

            if (c == null || username.Length < 1 || !re.IsMatch(password))
            {
                return(null);
            }

            u           = User.CreateUser(c.CreatorID, c.FirstName, c.LastName, username, SecurityManager.HashPasswordForStoringInDatabase(password), isStaff);
            g.Name      = username;
            g.IsSpecial = true;
            using (WindchimeEntities wce = new WindchimeEntities())
            {
                wce.AddToCreatorSet(u);
                wce.AddToGroups(g);
                g.Users.Add(u);
                wce.SaveChanges();
                wce.Detach(g);
                wce.Detach(u);
            }

            return(u);
        }