web.config配置文件如下:
<!--数据库连接字符串-->
<connectionStrings>
<add name="EmploymentUserConnectionString" connectionString="Data Source=.\sqlexpress;Initial Catalog=EmploymentUser;User ID=sa;Password=123456" providerName="System.Data.SqlClient"/>
</connectionStrings>
<!--
通过 <authentication> 节可以配置 ASP.NET 用来
识别进入用户的
安全身份验证模式。
-->
<!--身份验证模式-->
<authentication mode="Forms">
<forms loginUrl="~/login.aspx"></forms>
</authentication> <!--用户管理-->
<membership defaultProvider="AspNetSqlMembershipProvider">
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="EmploymentUserConnectionString" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="true" applicationName="/" requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression=""/>
</providers>
</membership>
<anonymousIdentification enabled="true" />
<profile defaultProvider="AspNetSqlProfileProvider" enabled="true" >
<providers>
<clear/>
<add name="AspNetSqlProfileProvider" connectionStringName="EmploymentUserConnectionString" applicationName="/" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</providers>
<properties>
<add name="newEnterprise" type="Enterprise" allowAnonymous="true" provider="AspNetSqlProfileProvider"/>
</properties>
<connectionStrings>
<add name="EmploymentUserConnectionString" connectionString="Data Source=.\sqlexpress;Initial Catalog=EmploymentUser;User ID=sa;Password=123456" providerName="System.Data.SqlClient"/>
</connectionStrings>
<!--
通过 <authentication> 节可以配置 ASP.NET 用来
识别进入用户的
安全身份验证模式。
-->
<!--身份验证模式-->
<authentication mode="Forms">
<forms loginUrl="~/login.aspx"></forms>
</authentication> <!--用户管理-->
<membership defaultProvider="AspNetSqlMembershipProvider">
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="EmploymentUserConnectionString" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="true" applicationName="/" requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression=""/>
</providers>
</membership>
<!--匿名用户-->
<anonymousIdentification enabled="true" />
<!--用户个性化信息-->
<profile defaultProvider="AspNetSqlProfileProvider" enabled="true" >
<providers>
<clear/>
<add name="AspNetSqlProfileProvider" connectionStringName="EmploymentUserConnectionString" applicationName="/" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</providers>
<properties>
<add name="newEnterprise" type="Enterprise" allowAnonymous="true" provider="AspNetSqlProfileProvider"/>
</properties>
</profile>
注意:
要把<authentication>节身份验证模式mode设置为Froms,否则Profile信息从匿名用户向注册用户迁移时会失败
mode 控制应用程序的默认身份验证模式。
- Windows 将 Windows 验证指定为默认的身份验证模式。当使用以下任意形式的 Microsoft Internet 信息服务 (IIS) 身份验证时使用该模式:基本、简要、集成的 Windows 验证 (NTLM/Kerberos) 或证书。Forms 将 ASP.NET 基于窗体的身份验证指定为默认的身份验证模式。
- Passport 将 Microsoft Passport 身份验证指定为默认的身份验证模式。
- None 不指定任何身份验证。只有匿名用户是预期的或者应用程序可以处理事件以提供其自身的身份验证。
Enterprise.cs文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
///企业注册信息
/// </summary>
public class Enterprise
{
private string enterpriseName; //企业名称
private string enterpriseIndustry; //企业所属行业
private string enterpriseAddress; //企业地址
private string enterpriseNature; //企业性质
private string enterpriseScale; //企业规模
private string enterprisePerson; //联系人
private string enterprisePhone; //联系电话
public Enterprise()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public Enterprise(string name,string industry,string address,string natrue,string scale,string person, string phone)
{
enterpriseName = name;
enterpriseIndustry = industry;
enterpriseAddress = address;
enterpriseNature = natrue;
enterpriseScale = scale;
enterprisePerson = person;
enterprisePhone = phone;
}
public string EnterpriseIndustry
{
get { return enterpriseIndustry; }
set { enterpriseIndustry = value; }
}
public string EnterpriseNature
{
get { return enterpriseNature; }
set { enterpriseNature = value; }
}
public string EnterpriseScale
{
get { return enterpriseScale; }
set { enterpriseScale = value; }
}
public string EnterpriseName
{
get { return enterpriseName; }
set { enterpriseName = value; }
}
public string EnterpriseAddress
{
get { return enterpriseAddress; }
set { enterpriseAddress = value; }
}
public string EnterprisePerson
{
get { return enterprisePerson; }
set { enterprisePerson = value; }
}
public string EnterprisePhone
{
get { return enterprisePhone; }
set { enterprisePhone = value; }
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
[Serializable]
///企业注册信息
/// </summary>
public class Enterprise
{
private string enterpriseName; //企业名称
private string enterpriseIndustry; //企业所属行业
private string enterpriseAddress; //企业地址
private string enterpriseNature; //企业性质
private string enterpriseScale; //企业规模
private string enterprisePerson; //联系人
private string enterprisePhone; //联系电话
public Enterprise()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public Enterprise(string name,string industry,string address,string natrue,string scale,string person, string phone)
{
enterpriseName = name;
enterpriseIndustry = industry;
enterpriseAddress = address;
enterpriseNature = natrue;
enterpriseScale = scale;
enterprisePerson = person;
enterprisePhone = phone;
}
public string EnterpriseIndustry
{
get { return enterpriseIndustry; }
set { enterpriseIndustry = value; }
}
public string EnterpriseNature
{
get { return enterpriseNature; }
set { enterpriseNature = value; }
}
public string EnterpriseScale
{
get { return enterpriseScale; }
set { enterpriseScale = value; }
}
public string EnterpriseName
{
get { return enterpriseName; }
set { enterpriseName = value; }
}
public string EnterpriseAddress
{
get { return enterpriseAddress; }
set { enterpriseAddress = value; }
}
public string EnterprisePerson
{
get { return enterprisePerson; }
set { enterprisePerson = value; }
}
public string EnterprisePhone
{
get { return enterprisePhone; }
set { enterprisePhone = value; }
}
}
注册用户前台Register.aspx:
<asp:CreateUserWizard ID="CreateUserWizard1" CssClass="createUser" runat="server"
Width="500px" OnContinueButtonClick="CreateUserWizard1_ContinueButtonClick" OnCreatedUser="CreateUserWizard1_CreatedUser"
BackColor="#EFF3FB" BorderColor="#B5C7DE" BorderStyle="Solid" BorderWidth="1px"
Font-Names="Verdana" Font-Size="12px">
<SideBarStyle BackColor="#507CD1" Font-Size="12px" VerticalAlign="Top" />
<SideBarButtonStyle BackColor="#507CD1" Font-Names="Verdana" ForeColor="White" />
<ContinueButtonStyle BackColor="White" BorderColor="#507CD1" BorderStyle="Solid"
Font-Size="12px" BorderWidth="1px" Font-Names="Verdana" ForeColor="#284E98" />
<NavigationButtonStyle BackColor="White" BorderColor="#507CD1" BorderStyle="Solid"
Font-Size="12px" BorderWidth="1px" Font-Names="Verdana" ForeColor="#284E98" />
<HeaderStyle BackColor="#284E98" BorderColor="#EFF3FB" BorderStyle="Solid" BorderWidth="2px"
Font-Bold="True" Font-Size="12px" ForeColor="White" HorizontalAlign="Center" />
<CreateUserButtonStyle BackColor="White" BorderColor="#507CD1" BorderStyle="Solid"
BorderWidth="1px" Font-Names="Verdana" ForeColor="#284E98" />
<TitleTextStyle BackColor="#507CD1" Font-Bold="True" Font-Size="14px" ForeColor="White" />
<StepStyle Font-Size="12px" />
<WizardSteps>
<asp:WizardStep Title="企业用户注册">
<table width="500px">
<tr>
<td colspan="2" align="center" style="background: #507CD1; font-weight: bold; font-size: 14px;
color: White;">
企业用户注册
</td>
</tr>
<tr>
<td align="right">
公司名称:
</td>
<td>
<asp:TextBox ID="txt_name" runat="server" Width="200px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="txt_name"
runat="server" ErrorMessage="必须填写“公司名称”。" ToolTip="必须填写“公司名称”。">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right">
公司所属行业:
</td>
<td>
<asp:DropDownList ID="ddl_industry" runat="server">
<asp:ListItem Selected="True">--请选择公司所属行业--</asp:ListItem>
<asp:ListItem>金融/银行/投资/基金/证券/保险</asp:ListItem>
<asp:ListItem>房地产/建筑/建材/工程建设</asp:ListItem>
<asp:ListItem>机械/电气设备/自动化/重工/轻工</asp:ListItem>
<asp:ListItem>>汽车/摩托车</asp:ListItem>
<asp:ListItem>石油/钢铁/电力/能源/煤矿</asp:ListItem>
<asp:ListItem>化工/生物/环保/农/林/牧/渔</asp:ListItem>
<asp:ListItem>医院/医疗/制药/护理/美容/保健</asp:ListItem>
<asp:ListItem>交通运输/物流仓储</asp:ListItem>
<asp:ListItem>仪器仪表/电器/家电/电子类</asp:ListItem>
<asp:ListItem>移动通信/电信运营、增值服务</asp:ListItem>
<asp:ListItem>通信设备/网络设备</asp:ListItem>
<asp:ListItem>电子技术/半导体/集成电路/芯片/仪器仪表</asp:ListItem>
<asp:ListItem>计算机硬件</asp:ListItem>
<asp:ListItem>计算机软件/IT服务(系统/数据/维护)</asp:ListItem>
<asp:ListItem>互联网/电子商务</asp:ListItem>
<asp:ListItem>咨询/法律/财会/中介服务</asp:ListItem>
<asp:ListItem>消费品/零售/服装/家具/贸易</asp:ListItem>
<asp:ListItem>媒体/广告/旅游/公关/酒店/会展/娱乐</asp:ListItem>
<asp:ListItem>国家机关/高校/研究所/事业单位/教育</asp:ListItem>
<asp:ListItem>跨领域经营/其它</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td align="right">
公司所在地址:
</td>
<td>
<asp:TextBox ID="txt_address" runat="server" Width="200px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" ControlToValidate="txt_address"
runat="server" ErrorMessage="必须填写“公司地址。”" ToolTip="必须填写“公司地址。”">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right">
公司性质:
</td>
<td>
<asp:DropDownList ID="ddl_nature" runat="server" Width="205px">
<asp:ListItem Selected="True">--请选择公司性质--</asp:ListItem>
<asp:ListItem>私营企业</asp:ListItem>
<asp:ListItem>国有企业</asp:ListItem>
<asp:ListItem>集体企业</asp:ListItem>
<asp:ListItem>有限责任公司</asp:ListItem>
<asp:ListItem>股份有限公司</asp:ListItem>
<asp:ListItem>中外合资企业</asp:ListItem>
<asp:ListItem>外商投资企业</asp:ListItem>
<asp:ListItem>其它</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td align="right">
公司规模:
</td>
<td>
<asp:DropDownList ID="ddl_scale" runat="server" Width="205px">
<asp:ListItem Selected="True">--请选择公司规模--</asp:ListItem>
<asp:ListItem>10人以下</asp:ListItem>
<asp:ListItem>50人以下</asp:ListItem>
<asp:ListItem>50~100人</asp:ListItem>
<asp:ListItem>100~200人</asp:ListItem>
<asp:ListItem>200~300人</asp:ListItem>
<asp:ListItem>300~400人</asp:ListItem>
<asp:ListItem>400~500人</asp:ListItem>
<asp:ListItem>500以上</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td align="right">
联系人:
</td>
<td>
<asp:TextBox ID="txt_person" runat="server" Width="200px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" ControlToValidate="txt_person"
runat="server" ErrorMessage="必须填写“联系人”。" ToolTip="必须填写“联系人”。">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right">
联系电话:
</td>
<td>
<asp:TextBox ID="txt_phone" runat="server" Width="200px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" ControlToValidate="txt_phone"
runat="server" ErrorMessage="必须填写“联系电话”。" ToolTip="必须填写“联系电话”。">*</asp:RequiredFieldValidator>
</td>
</tr>
</table>
</asp:WizardStep>
<asp:CreateUserWizardStep runat="server" Title="企业用户注册">
<ContentTemplate>
<table border="0" style="font-family: Verdana; font-size: 100%; width: 500px;">
<tr>
<td align="center" colspan="2" style="color: White; background-color: #507CD1; font-size: 14px;
font-weight: bold;">
企业用户注册
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">用户名:</asp:Label>
</td>
<td>
<asp:TextBox ID="UserName" runat="server" Width="200px"></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" Width="200px">密码:</asp:Label>
</td>
<td>
<asp:TextBox ID="Password" runat="server" TextMode="Password" Width="200px"></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" Width="200px"></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" Width="200px"></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" Width="200px"></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" Width="200px"></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">
</asp:CompleteWizardStep>
</WizardSteps>
</asp:CreateUserWizard>
后台:
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; public partial class EnterpriseRegister : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
{
Response.Redirect("login.aspx");
/// 把企业个性化信息存入profile中
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
Roles.AddUserToRole(CreateUserWizard1.UserName, "企业用户");
Profile.newEnterprise.EnterpriseName = txt_name.Text; //企业名称
Profile.newEnterprise.EnterpriseIndustry = ddl_industry.SelectedValue; //所属行业
Profile.newEnterprise.EnterpriseAddress = txt_address.Text; //企业地址
Profile.newEnterprise.EnterpriseNature = ddl_nature.SelectedValue; //企业性质
Profile.newEnterprise.EnterpriseScale = ddl_scale.SelectedValue; //企业规模
Profile.newEnterprise.EnterprisePerson = txt_person.Text; //联系人
Profile.newEnterprise.EnterprisePhone = txt_phone.Text; //联系电话
Profile.Save();
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security; public partial class EnterpriseRegister : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
{
Response.Redirect("login.aspx");
}
/// 把企业个性化信息存入profile中
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
Roles.AddUserToRole(CreateUserWizard1.UserName, "企业用户");
Profile.newEnterprise.EnterpriseName = txt_name.Text; //企业名称
Profile.newEnterprise.EnterpriseIndustry = ddl_industry.SelectedValue; //所属行业
Profile.newEnterprise.EnterpriseAddress = txt_address.Text; //企业地址
Profile.newEnterprise.EnterpriseNature = ddl_nature.SelectedValue; //企业性质
Profile.newEnterprise.EnterpriseScale = ddl_scale.SelectedValue; //企业规模
Profile.newEnterprise.EnterprisePerson = txt_person.Text; //联系人
Profile.newEnterprise.EnterprisePhone = txt_phone.Text; //联系电话
Profile.Save();
}
}
Global.asax文件中:
void Profile_MigrateAnonymous(object sender, ProfileMigrateEventArgs e)
{
//获取匿名用户的Profile对象
ProfileCommon anonProfile = Profile.GetProfile(e.AnonymousID);
//注意;Profile.newEnterprise存放的是登陆后的数据
//anonProfile.newEnterprise是该用户在匿名状态下添加的数据
if (!string.IsNullOrEmpty(anonProfile.newEnterprise.EnterpriseName))
{
Profile.newEnterprise = anonProfile.newEnterprise;
}
//删除匿名用户的用户数据(从aspnet_Users表)
//Membership.DeleteUser(e.AnonymousID);
//删除匿名用户的Profle数据(从aspnet_Profile表)
ProfileManager.DeleteProfile(e.AnonymousID);
//删除匿名用户标识
AnonymousIdentificationModule.ClearAnonymousIdentifier();
Profile.Save();
{
//获取匿名用户的Profile对象
ProfileCommon anonProfile = Profile.GetProfile(e.AnonymousID);
//注意;Profile.newEnterprise存放的是登陆后的数据
//anonProfile.newEnterprise是该用户在匿名状态下添加的数据
if (!string.IsNullOrEmpty(anonProfile.newEnterprise.EnterpriseName))
{
Profile.newEnterprise = anonProfile.newEnterprise;
}
//删除匿名用户的用户数据(从aspnet_Users表)
//Membership.DeleteUser(e.AnonymousID);
//删除匿名用户的Profle数据(从aspnet_Profile表)
ProfileManager.DeleteProfile(e.AnonymousID);
//删除匿名用户标识
AnonymousIdentificationModule.ClearAnonymousIdentifier();
Profile.Save();
}