Esempio n. 1
0
        protected void btnSaveUserInfo_Click(object sender, EventArgs e)
        {
            string userName = txtUserName.Text.Trim();
            string password = txtUserPassword.Text;
            string email    = txtEmail.Text.Trim();
            bool   status   = chkSetActive.Checked;

            // 创建用户
            MembershipUser user = null;

            try
            {
                user = Membership.CreateUser(userName, password, email);
            }
            catch (Exception ex)
            {
                lblResult.Text = "创建用户失败:" + ex.Message;
                return;
            }

            // 修养用户状态
            user.IsApproved = status;
            user.Email      = email;
            Membership.UpdateUser(user);

            // 保存用户扩展信息
            AspnetUserExtInfo profile = new AspnetUserExtInfo();

            profile.UserName  = user.UserName;
            profile.TrueName  = txtTrueName.Text.Trim();
            profile.Email     = txtEmail.Text.Trim();
            profile.QQ        = txtQQ.Text.Trim();
            profile.MSN       = txtMSN.Text.Trim();
            profile.Mobile    = txtMobile.Text.Trim();
            profile.IdCard    = txtIdCard.Text.Trim();
            profile.TelePhone = txtPhone.Text.Trim();
            AspnetUserExt.SaveUserExt(profile);

            // 修改用户所属角色
            foreach (ListItem item in chkRoles.Items)
            {
                if (item.Selected)
                {
                    if (!Roles.IsUserInRole(user.UserName, item.Text))
                    {
                        Roles.AddUserToRole(user.UserName, item.Text);
                    }
                }
                else
                {
                    if (Roles.IsUserInRole(user.UserName, item.Text))
                    {
                        Roles.RemoveUserFromRole(user.UserName, item.Text);
                    }
                }
            }

            lblResult.Text = "用户创建成功";
        }
        /// <summary>
        /// 保存用户基本信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveUserInfo_Click(object sender, EventArgs e)
        {
            AspnetUserExtInfo user = new AspnetUserExtInfo();

            user.UserName  = lblUserId.Text;
            user.TrueName  = txtTrueName.Text.Trim();
            user.Email     = txtEmail.Text.Trim();
            user.QQ        = txtQQ.Text.Trim();
            user.MSN       = txtMSN.Text.Trim();
            user.Mobile    = txtMobile.Text.Trim();
            user.IdCard    = txtIdCard.Text.Trim();
            user.TelePhone = txtPhone.Text.Trim();

            if (AspnetUserExt.SaveUserExt(user))
            {
                lblSaveUserInfoResult.Text = "保存成功";
            }
            else
            {
                lblSaveUserInfoResult.Text = "保存失败";
            }
        }