添加用户3

自写代码

前台
  <fieldset style="height:500px">
        <legend>用户注册信息</legend>
        <table border="0" cellpadding="0" cellspacing="0">
            <tr>
                <td align="right">
                    <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">用户名:</asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="UserName" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"
                        ErrorMessage="用户名必须填写." ToolTip="用户名必须填写." ValidationGroup="CreateUser">*</asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td align="right">
                    <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">密码:</asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"
                        ErrorMessage="Password is required." ToolTip="Password is required." ValidationGroup="CreateUser">*</asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td align="right">
                    <asp:Label ID="ConfirmPasswordLabel" runat="server" AssociatedControlID="ConfirmPassword">确认密码:</asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="ConfirmPassword" runat="server" TextMode="Password"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="ConfirmPasswordRequired" runat="server" ControlToValidate="ConfirmPassword"
                        ErrorMessage="Confirm Password is required." ToolTip="Confirm Password is required."
                        ValidationGroup="CreateUser">*</asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td align="right">
                    <asp:Label ID="EmailLabel" runat="server" AssociatedControlID="Email">E-mail:</asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="Email" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="EmailRequired" runat="server" ControlToValidate="Email"
                        ErrorMessage="E-mail is required." ToolTip="E-mail is required." ValidationGroup="CreateUser">*</asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td align="center" colspan="2">
                    <asp:CompareValidator ID="PasswordCompare" runat="server" ControlToCompare="Password"
                        ControlToValidate="ConfirmPassword" Display="Dynamic" ErrorMessage="The Password and Confirmation Password must match."
                        ValidationGroup="CreateUser"></asp:CompareValidator>
                </td>
            </tr>
        </table>
    </fieldset>
    
<fieldset>
<legend>用户角色指定</legend>


    <asp:CheckBoxList ID="CheckBoxListRoles" runat="server"
     RepeatDirection="Horizontal"
    >
    </asp:CheckBoxList>


</fieldset>


<div class="boxGreen">
    <asp:Button ID="ButtonRegister" runat="server" Text="注册用户"  
        ValidationGroup="CreateUser" onclick="ButtonRegister_Click" />
    <asp:Label ID="LabelInfo" runat="server" Text=""></asp:Label>
</div>

后台

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;


namespace WebApplication1
{
    public partial class NewUsers2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.CheckBoxListRoles.DataSource = Roles.GetAllRoles();
                this.CheckBoxListRoles.DataBind();
            }


        }


        protected void ButtonRegister_Click(object sender, EventArgs e)
        {
            //1.api注册
        MembershipCreateStatus result;
     var user=   Membership.CreateUser(this.UserName.Text, this.Password.Text, this.Email.Text, "?", "?", true, out result);
        
        //2.roles管理


     if (result == MembershipCreateStatus.Success)
     {
         string username = user.UserName;
         foreach (ListItem item in this.CheckBoxListRoles.Items)
         {
             if (item.Selected)
             {
                 string role = item.Text;
                 Roles.AddUserToRole(username, role);
             }
         }
         this.LabelInfo.Text = "创建完毕";
     }
     else
     {
         this.LabelInfo.Text = string.Format("创建错误:{0}",        result);
     }
        //3.显示结果
    }
        
    }
}

原文地址:https://www.cnblogs.com/shuozi-love/p/3746698.html