Profile扩展

web.config

<system.web>

  <!-- 支持匿名访问,这是在需要授权时,对没有经过身份验证的实体进行标识所必需的 -->
   <anonymousIdentification enabled="true"/>

  <!-- 设置“成员/角色管理”权限规则 -->
   <profile defaultProvider="AspNetSqlProfileProvider" enabled ="true">
    <providers>
     <remove name="AspNetSqlProfileProvider" />
     <add name="AspNetSqlProfileProvider" connectionStringName="LocalSqlServer" applicationName="/" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </providers>
    <properties>
     <add name="FirstName" type="System.String" allowAnonymous="true" />
     <add name="LastName" type="System.String" allowAnonymous="true" />
    </properties>
   </profile>


</system.web>

aspx

    <fieldset>
        <legend>用户注册</legend>
        <asp:CreateUserWizard ID="CreateUserWizard1" runat="server" OnCreatedUser="CreateUserWizard1_CreatedUser" OnContinueButtonClick="CreateUserWizard1_ContinueButtonClick">
            <WizardSteps>

                <asp:WizardStep>
                        <asp:Label ID="lb_firstname" Text="FirstName:" AssociatedControlID="tb_firstname" runat="server" />
                        <asp:TextBox ID="tb_firstname" runat="server" />
                        <br />
                        <asp:Label ID="lb_lastname" Text="LastName:" AssociatedControlID="tb_lastname" runat="server" />
                        <asp:TextBox ID="tb_lastname" TextMode="Password" runat="server" />
                </asp:WizardStep>

                <asp:CreateUserWizardStep />

            </WizardSteps>
        </asp:CreateUserWizard>
    </fieldset>

aspx.cs

    protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
    {
            Profile.FirstName=tb_firstname.Text;
            Profile.LastName=tb_lastname.Text;
            Profile.Save();
    }

    protected void CreateUserWizard1_ContinueButtonClick(object sender, EventArgs e)
    {
        Response.Redirect("register.aspx");
    }

///另外的方式在在自己的表中注册
//

aspx

    <fieldset>
        <legend>用户注册</legend>
        <asp:CreateUserWizard ID="CreateUserWizard1" runat="server" OnCreatedUser="CreateUserWizard1_CreatedUser">
            <WizardSteps>
                <asp:WizardStep>
                    <asp:Label ID="lblFirstName" Text="First Name:" AssociatedControlID="txtFirstName"
                        runat="server" />
                    <br />
                    <asp:TextBox ID="txtFirstName" runat="server" />
                    <br />
                    <br />
                    <asp:Label ID="lblLastName" Text="Last Name:" AssociatedControlID="txtLastName" runat="server" />
                    <br />
                    <asp:TextBox ID="txtLastName" runat="server" />
                </asp:WizardStep>
                <asp:CreateUserWizardStep />
            </WizardSteps>
        </asp:CreateUserWizard>
    </fieldset>

aspx.cs

    using System;
    using System.Data.SqlClient;
    using System.Web.Configuration;

    protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
    {
        CreateUserProfile(CreateUserWizard1.UserName,txtFirstName.Text,txtLastName.Text);
    }

    private void CreateUserProfile(string userName, string firstName, string lastName)
    {
        string conString = WebConfigurationManager.
        ConnectionStrings["LocalSqlServer"].ConnectionString;
        SqlConnection con = new SqlConnection(conString);
        SqlCommand cmd = new SqlCommand("INSERT aspnet_diy_profile (UserName,FirstName,LastName) VALUES (@UserName,@FirstName, @LastName)", con);
        cmd.Parameters.AddWithValue("@UserName", userName);
        cmd.Parameters.AddWithValue("@FirstName", firstName);
        cmd.Parameters.AddWithValue("@LastName", lastName);
        using (con)
        {
            con.Open();
            cmd.ExecuteNonQuery();
        }
    }

 

posted @ 2010-08-09 11:29  midisong  阅读(163)  评论(0编辑  收藏  举报