qq_35076105 2021-12-10 09:47 采纳率: 50%
浏览 18
已结题

ASP用户管理 CreateUserWizard如何把数据存放到自己创建的数据库的用户表中

在制作网站用户注册界面时遇到的问题

<asp:CreateUserWizard ID="CreateUserWizard1" runat="server" Width="1400px" style="margin-right: 0px; margin-top: 0px; background-image: url('');" Height="383px" OnCreatedUser="CreateUserWizard1_CreatedUser">
    <WizardSteps>
        <asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server" >
            <ContentTemplate>
                <table style="font-size: 100%; height: 383px; width: 1400px;">
                    <tr>
                        <td align="center" colspan="2">注册新帐户</td>
                    </tr>
                    <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="CreateUserWizard1">*</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="必须填写“密码”。" ToolTip="必须填写“密码”。" ValidationGroup="CreateUserWizard1">*</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="必须填写“确认密码”。" ToolTip="必须填写“确认密码”。" ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                        </td>
                    </tr>
                    <tr>
                        <td align="right">
                            <asp:Label ID="EmailLabel" runat="server" AssociatedControlID="Email">电子邮件:</asp:Label>
                        </td>
                        <td>
                            <asp:TextBox ID="Email" runat="server"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="EmailRequired" runat="server" ControlToValidate="Email" ErrorMessage="必须填写“电子邮件”。" ToolTip="必须填写“电子邮件”。" ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                        </td>
                    </tr>
                    <tr>
                        <td align="right">
                            <asp:Label ID="QuestionLabel" runat="server" AssociatedControlID="Question">安全提示问题:</asp:Label>
                        </td>
                        <td>
                            <asp:TextBox ID="Question" runat="server"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="QuestionRequired" runat="server" ControlToValidate="Question" ErrorMessage="必须填写“安全提示问题”。" ToolTip="必须填写“安全提示问题”。" ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                        </td>
                    </tr>
                    <tr>
                        <td align="right">
                            <asp:Label ID="AnswerLabel" runat="server" AssociatedControlID="Answer">安全答案:</asp:Label>
                        </td>
                        <td>
                            <asp:TextBox ID="Answer" runat="server"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="AnswerRequired" runat="server" ControlToValidate="Answer" ErrorMessage="必须填写“安全答案”。" ToolTip="必须填写“安全答案”。" ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                        </td>
                    </tr>
                    <tr>
                        <td align="center" colspan="2">
                            <asp:CompareValidator ID="PasswordCompare" runat="server" ControlToCompare="Password" ControlToValidate="ConfirmPassword" Display="Dynamic" ErrorMessage="“密码”和“确认密码”必须匹配。" ValidationGroup="CreateUserWizard1"></asp:CompareValidator>
                        </td>
                    </tr>
                    <tr>
                        <td align="center" colspan="2" style="color: Red;">
                            <asp:Literal ID="ErrorMessage" runat="server" EnableViewState="False"></asp:Literal>
                        </td>
                    </tr>
                </table>
            </ContentTemplate>
        </asp:CreateUserWizardStep>
        <asp:CompleteWizardStep runat="server" >
            <ContentTemplate>
                <table style="height: 383px; width: 1400px; font-size: 100%;">
                    <tr>
                        <td align="center">完成</td>
                    </tr>
                    <tr>
                        <td>已成功创建您的帐户。</td>
                    </tr>
                    <tr>
                        <td align="right">
                            <asp:Button ID="ContinueButton" runat="server" CausesValidation="False" CommandName="Continue" Text="继续" ValidationGroup="CreateUserWizard1" />
                        </td>
                    </tr>
                </table>
            </ContentTemplate>
        </asp:CompleteWizardStep>
    </WizardSteps>
</asp:CreateUserWizard>

用户表

CREATE TABLE [user1] ([id] int not null primary key,
    [password] char(16),
    [sex] char(2),
    [email] char(50))

运行结果及报错内容

在网上看了一些Membership相关的资料,还是不会写

注册用户的数据直接添加到user1表中

  • 写回答

1条回答 默认 最新

  • Yijing Sun 2021-12-10 15:02
    关注

    你好,
    请问你是哪里不会.接下来我将把步骤一步一步展示给你:
    第一步:新建项目:
    创建一个空网站,创建sql 表(我相信你已经做过了)
    第二步:配置连接字符串:
    在你的项目文件夹下面,有一个web.config 的文件, 你在该文件下面添加连接字符串, 这个字符串是连接你的数据库的. 字符串如下:

     <connectionStrings>
        <add name="ApplicationServices" connectionString="Data Source=服务器名;Initial Catalog=数据库名; User ID=用户ID;Password=用户密码" providerName="System.Data.SqlClient"/>
      </connectionStrings>
    

    第三步:配置身份验证:
    身份认证分三种:Windows 身份验证,.NET 护照身份验证,表单认证. 在 web.config 文件中添加以下部分以启用身份验证。这里假设用的表单身份验证.

    <system.web>
        <authentication mode="Forms">
          <forms loginUrl="~/Account/Login.aspx" timeout="2880" cookieless="AutoDetect">
          </forms>
        </authentication>
      </system.web>
    

    第四步:
    你需要设置密码的格式:散列、加密和明文. 在web.config中添加以下代码:

    <system.web>
       <membership>
          <providers>
            <clear/>     
            <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/"/>
          <providers>
        <membership>
    <system.web>
    

    第五步:
    添加登录表单,页面名称Register.aspx.第一次可以转到Register.aspx页面注册用户. 在这里使用CreateUserText="Register" CreateUserUrl="~/Register.aspx"

    <asp:Login ID="loginUser" runat="server" CreateUserText="Register"
      CreateUserUrl="~/Register.aspx"
      onauthenticate="loginUser_Authenticate" BackColor="#F7F7DE"
      BorderColor="#CCCC99" BorderStyle="Solid" BorderWidth="1px"
      Font-Names="Verdana" Font-Size="10pt" Height="135px" Width="286px">
      <TitleTextStyle BackColor="#6B696B" Font-Bold="True" ForeColor="#FFFFFF" />
    <asp:Login>
    

    添加以下代码片段以处理相应的 onauthenticate 事件:

    protected void loginUser_Authenticate(object sender, AuthenticateEventArgs e)
    {
        bool isLogin = Membership.ValidateUser(loginUser.UserName, loginUser.Password);
        if (isLogin)
        {
          loginUser.Visible = true;
          Session["user"] = User.Identity.Name;
          FormsAuthentication.RedirectFromLoginPage(loginUser.UserName, true);
          Response.Redirect("Default.aspx");
        }
    }
    

    登录成功后。页面将被重定向到 Default.aspx 页面。所以我们需要添加一个 Default.aspx 页面.
    第六步:
    添加注册表单, 你已经写了.

    <asp:CreateUserWizard ID="RegisterUser" runat="server"
                OnCreatedUser="RegisterUser_CreatedUser" BackColor="#E3EAEB"
                BorderColor="#E6E2D8" BorderStyle="Solid" BorderWidth="1px"
                Font-Names="Verdana" Font-Size="0.8em">
                <ContinueButtonStyle BackColor="White" BorderColor="#C5BBAF"
                    BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana"
                    ForeColor="#1C5E55" />
                <CreateUserButtonStyle BackColor="White" BorderColor="#C5BBAF"
                    BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana"
                    ForeColor="#1C5E55" />
                <TitleTextStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                <WizardSteps>
                    <asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server">
                    asp:CreateUserWizardStep>
                    <asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server">
                    asp:CompleteWizardStep>
                WizardSteps>
                <HeaderStyle BackColor="#666666" BorderColor="#E6E2D8" BorderStyle="Solid"
                    BorderWidth="2px" Font-Bold="True" Font-Size="0.9em" ForeColor="White"
                    HorizontalAlign="Center" />
                <NavigationButtonStyle BackColor="White" BorderColor="#C5BBAF"
                    BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana"
                    ForeColor="#1C5E55" />
                <SideBarButtonStyle ForeColor="White" />
                <SideBarStyle BackColor="#1C5E55" Font-Size="0.9em" VerticalAlign="Top" />
                <StepStyle BorderWidth="0px" />
            <asp:CreateUserWizard>
    

    添加以下代码片段以处理相应的 OnCreatedUser 事件。

    protected void RegisterUser_CreatedUser(object sender, EventArgs e)
    {
        FormsAuthentication.SetAuthCookie(RegisterUser.UserName, false /* createPersistentCookie */);
        
        string continueUrl = RegisterUser.ContinueDestinationPageUrl;
        if (String.IsNullOrEmpty(continueUrl))
        {
            continueUrl = "Login.aspx";
        }
        Response.Redirect(continueUrl);
    }
    

    第七步:
    为注册页面添加位置标签:在 web.config 文件中添加以下代码片段。这意味着所有人都可以注册页面。因为,第一次您必须在不登录应用程序的情况下创建用户。

    <locationpath="Register.aspx">
      <system.web>
        <authorization>
          <allowusers="*"/>
        </authorization>
      </system.web>
    </location>
    

    第八步:
    添加默认页面,添加 Default.aspx 页面。您可以在此处添加一些消息,因为登录成功后页面将重定向到默认页面。

    如果你是不会写后台代码插入的话,就是获取前台的数据,然后使用insert 语句.

    sqlStmt = 'insert into user1(password,sex,Email) Values (@password,@sex,@Email)  ';
        conString = 'Data Source=服务器名;Initial Catalog=数据库名; User ID=用户ID;Password=用户密码" providerName="System.Data.SqlClient';
        cn = new SqlConnection(conString);
        cmd = new SqlCommand(sqlStmt, cn);
        cmd.Parameters.Add(new SqlParameter('@password', SqlDbType.NVarChar, 11));
        cmd.Parameters.Add(new SqlParameter('@sex', SqlDbType.NVarChar, 40));
        cmd.Parameters.Add(new SqlParameter('@Email', SqlDbType.NVarChar,40));
        cmd.Parameters['@Password'].Value = Password.Text;
        cmd.Parameters['@sex'].Value = sex.Text;
        cmd.Parameters['@Email'].Value = Email.Text;
        cn.Open();
        cmd.ExecuteNonQuery();
    

    有帮助麻烦点下【采纳该答案】,谢谢~~有其他问题可以继续交流~

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 12月18日
  • 已采纳回答 12月10日
  • 创建了问题 12月10日

悬赏问题

  • ¥15 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败