成员身份API通过两个新的类来实现:Membership和MembershipUser,它们是一对很好的配合搭档。前者包含了用于创建用户、验证用户以及完成其他工作(找回一个membershipUser实例,更新一个membershipUser实例,通过不同条件寻找一个用户,获得当前在线用户数量,删除一个已经不再需要的账户)的静态方法。MembershipUser代表当个用户,它包含了用于检索和更改密码、获得上次登录日期以及完成类似工作方法和属性。
Membership的CreateUser方法:
1、注册数据库,打开c:\windows\microsoft.net\framework\v2.05727\aspnet_regsql.exe接着就是一步步的把数据库创建。这里我自己创建了一个名为membershipdemo数据库。
2、在web.config配置文件对membership进行相关设置。
1
<
membership
defaultProvider
="AspNetSqlMembershipProvider"
userIsOnlineTimeWindow
="15"
hashAlgorithmType
=""
>
2 < providers >
3 < clear />
4 < add connectionStringName ="ConnectionString"
5 enablePasswordRetrieval ="false"
6 enablePasswordReset ="true"
7 requiresQuestionAndAnswer ="true"
8 applicationName ="/"
9 requiresUniqueEmail ="false"
10 passwordFormat ="Hashed"
11 maxInvalidPasswordAttempts ="5"
12 minRequiredPasswordLength ="7"
13 minRequiredNonalphanumericCharacters ="1"
14 passwordAttemptWindow ="10"
15 passwordStrengthRegularExpression =""
16 name ="AspNetSqlMembershipProvider"
17 type ="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
18 </ providers >
19 </ membership >
2 < providers >
3 < clear />
4 < add connectionStringName ="ConnectionString"
5 enablePasswordRetrieval ="false"
6 enablePasswordReset ="true"
7 requiresQuestionAndAnswer ="true"
8 applicationName ="/"
9 requiresUniqueEmail ="false"
10 passwordFormat ="Hashed"
11 maxInvalidPasswordAttempts ="5"
12 minRequiredPasswordLength ="7"
13 minRequiredNonalphanumericCharacters ="1"
14 passwordAttemptWindow ="10"
15 passwordStrengthRegularExpression =""
16 name ="AspNetSqlMembershipProvider"
17 type ="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
18 </ providers >
19 </ membership >
3、配置相应的连接字符串。
1
<
connectionStrings
>
2 < add name ="ConnectionString" connectionString ="server=(local);database=membershipdemo;uid=sa;pwd=123456" />
3 </ connectionStrings >
2 < add name ="ConnectionString" connectionString ="server=(local);database=membershipdemo;uid=sa;pwd=123456" />
3 </ connectionStrings >
4、在新的项目文件页面中定义如下图表格:
5、创建用户事件,代码如下:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1
public
void
CreateUser_OnClick(
object
sender, EventArgs args)
2 {
3 try
4 {
5 MembershipCreateStatus status;
6 MembershipUser newUser = Membership.CreateUser(UsernameTextbox.Text, PasswordTextbox.Text,EmailTextbox.Text,PasswordQuestionTextbox.Text,PasswordAnswerTextbox.Text, true , out status);
7
8 Response.Redirect( " login.aspx " );
9 }
10 catch (MembershipCreateUserException e)
11 {
12 Msg.Text = GetErrorMessage(e.StatusCode);
13 }
14 catch (HttpException e)
15 {
16 Msg.Text = e.Message;
17 }
18 }
2 {
3 try
4 {
5 MembershipCreateStatus status;
6 MembershipUser newUser = Membership.CreateUser(UsernameTextbox.Text, PasswordTextbox.Text,EmailTextbox.Text,PasswordQuestionTextbox.Text,PasswordAnswerTextbox.Text, true , out status);
7
8 Response.Redirect( " login.aspx " );
9 }
10 catch (MembershipCreateUserException e)
11 {
12 Msg.Text = GetErrorMessage(e.StatusCode);
13 }
14 catch (HttpException e)
15 {
16 Msg.Text = e.Message;
17 }
18 }
6、出错处理(这是借鉴他人的):
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1
public
string
GetErrorMessage(MembershipCreateStatus status)
2 {
3 switch (status)
4 {
5 case MembershipCreateStatus.DuplicateUserName:
6 return " Username already exists. Please enter a different user name. " ;
7
8 case MembershipCreateStatus.DuplicateEmail:
9 return " A username for that e-mail address already exists. Please enter a different e-mail address. " ;
10
11 case MembershipCreateStatus.InvalidPassword:
12 return " The password provided is invalid. Please enter a valid password value. " ;
13
14 case MembershipCreateStatus.InvalidEmail:
15 return " The e-mail address provided is invalid. Please check the value and try again. " ;
16
17 case MembershipCreateStatus.InvalidAnswer:
18 return " The password retrieval answer provided is invalid. Please check the value and try again. " ;
19
20 case MembershipCreateStatus.InvalidQuestion:
21 return " The password retrieval question provided is invalid. Please check the value and try again. " ;
22
23 case MembershipCreateStatus.InvalidUserName:
24 return " The user name provided is invalid. Please check the value and try again. " ;
25
26 case MembershipCreateStatus.ProviderError:
27 return " The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator. " ;
28
29 case MembershipCreateStatus.UserRejected:
30 return " The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator. " ;
31
32 default :
33 return " An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator. " ;
34 }
35 }
2 {
3 switch (status)
4 {
5 case MembershipCreateStatus.DuplicateUserName:
6 return " Username already exists. Please enter a different user name. " ;
7
8 case MembershipCreateStatus.DuplicateEmail:
9 return " A username for that e-mail address already exists. Please enter a different e-mail address. " ;
10
11 case MembershipCreateStatus.InvalidPassword:
12 return " The password provided is invalid. Please enter a valid password value. " ;
13
14 case MembershipCreateStatus.InvalidEmail:
15 return " The e-mail address provided is invalid. Please check the value and try again. " ;
16
17 case MembershipCreateStatus.InvalidAnswer:
18 return " The password retrieval answer provided is invalid. Please check the value and try again. " ;
19
20 case MembershipCreateStatus.InvalidQuestion:
21 return " The password retrieval question provided is invalid. Please check the value and try again. " ;
22
23 case MembershipCreateStatus.InvalidUserName:
24 return " The user name provided is invalid. Please check the value and try again. " ;
25
26 case MembershipCreateStatus.ProviderError:
27 return " The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator. " ;
28
29 case MembershipCreateStatus.UserRejected:
30 return " The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator. " ;
31
32 default :
33 return " An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator. " ;
34 }
35 }