1.创建一个Class Library项目,让自定义CustomMemberShipProvider类继承于System.Web.Security.MembershipProvider类。
1
public
class
CustomMemberShipProvider : MembershipProvider
2 {
3 private bool requiresQuestionAndAnswer;
4 private int minRequiredPasswordLength;
5
6 public override string ApplicationName
7 {
8 get
9 {
10 throw new Exception( " The method or operation is not implemented. " );
11 }
12 set
13 {
14 throw new Exception( " The method or operation is not implemented. " );
15 }
16 }
17 public override void Initialize( string name, System.Collections.Specialized.NameValueCollection config)
18 {
19
20 if (config[ " requiresQuestionAndAnswer " ].ToLower() == " true " )
21 requiresQuestionAndAnswer = true ;
22 else
23 requiresQuestionAndAnswer = false ;
24 int .TryParse(config[ " minPasswordLength " ], out minRequiredPasswordLength);
25 base .Initialize(name, config);
26 }
27 public override bool ChangePassword( string username, string oldPassword, string newPassword)
28 {
29 return true ;
30 }
31
32 public override bool ChangePasswordQuestionAndAnswer( string username, string password, string newPasswordQuestion, string newPasswordAnswer)
33 {
34 throw new Exception( " The method or operation is not implemented. " );
35 }
36
37 public override MembershipUser CreateUser( string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
38 {
39 throw new Exception( " The method or operation is not implemented. " );
40 }
41
42 public override bool DeleteUser( string username, bool deleteAllRelatedData)
43 {
44 throw new Exception( " The method or operation is not implemented. " );
45 }
46
47 public override bool EnablePasswordReset
48 {
49 get { throw new Exception( " The method or operation is not implemented. " ); }
50 }
51
52 public override bool EnablePasswordRetrieval
53 {
54 get { throw new Exception( " The method or operation is not implemented. " ); }
55 }
56
57 public override MembershipUserCollection FindUsersByEmail( string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
58 {
59 throw new Exception( " The method or operation is not implemented. " );
60 }
61
62 public override MembershipUserCollection FindUsersByName( string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
63 {
64 throw new Exception( " The method or operation is not implemented. " );
65 }
66
67 public override MembershipUserCollection GetAllUsers( int pageIndex, int pageSize, out int totalRecords)
68 {
69 throw new Exception( " The method or operation is not implemented. " );
70 }
71
72 public override int GetNumberOfUsersOnline()
73 {
74 throw new Exception( " The method or operation is not implemented. " );
75 }
76
77 public override string GetPassword( string username, string answer)
78 {
79 throw new Exception( " The method or operation is not implemented. " );
80 }
81
82 public override MembershipUser GetUser( string username, bool userIsOnline)
83 {
84 return null ;
85 }
86
87 public override MembershipUser GetUser( object providerUserKey, bool userIsOnline)
88 {
89 throw new Exception( " The method or operation is not implemented. " );
90 }
91
92 public override string GetUserNameByEmail( string email)
93 {
94 throw new Exception( " The method or operation is not implemented. " );
95 }
96
97 public override int MaxInvalidPasswordAttempts
98 {
99 get { throw new Exception( " The method or operation is not implemented. " ); }
100 }
101
102 public override int MinRequiredNonAlphanumericCharacters
103 {
104 get { return 0 ; }
105 }
106
107 public override int MinRequiredPasswordLength
108 {
109 get { return minRequiredPasswordLength; }
110 }
111
112 public override int PasswordAttemptWindow
113 {
114 get { throw new Exception( " The method or operation is not implemented. " ); }
115 }
116
117 public override MembershipPasswordFormat PasswordFormat
118 {
119 get { throw new Exception( " The method or operation is not implemented. " ); }
120 }
121
122 public override string PasswordStrengthRegularExpression
123 {
124 get { throw new Exception( " The method or operation is not implemented. " ); }
125 }
126
127 public override bool RequiresQuestionAndAnswer
128 {
129 get { return requiresQuestionAndAnswer; }
130 }
131
132 public override bool RequiresUniqueEmail
133 {
134 get { throw new Exception( " The method or operation is not implemented. " ); }
135 }
136
137 public override string ResetPassword( string username, string answer)
138 {
139 throw new Exception( " The method or operation is not implemented. " );
140 }
141
142 public override bool UnlockUser( string userName)
143 {
144 throw new Exception( " The method or operation is not implemented. " );
145 }
146
147 public override void UpdateUser(MembershipUser user)
148 {
149 throw new Exception( " The method or operation is not implemented. " );
150 }
151
152 public override bool ValidateUser( string username, string password)
153 {
154 return true ;
155 }
156 }
2 {
3 private bool requiresQuestionAndAnswer;
4 private int minRequiredPasswordLength;
5
6 public override string ApplicationName
7 {
8 get
9 {
10 throw new Exception( " The method or operation is not implemented. " );
11 }
12 set
13 {
14 throw new Exception( " The method or operation is not implemented. " );
15 }
16 }
17 public override void Initialize( string name, System.Collections.Specialized.NameValueCollection config)
18 {
19
20 if (config[ " requiresQuestionAndAnswer " ].ToLower() == " true " )
21 requiresQuestionAndAnswer = true ;
22 else
23 requiresQuestionAndAnswer = false ;
24 int .TryParse(config[ " minPasswordLength " ], out minRequiredPasswordLength);
25 base .Initialize(name, config);
26 }
27 public override bool ChangePassword( string username, string oldPassword, string newPassword)
28 {
29 return true ;
30 }
31
32 public override bool ChangePasswordQuestionAndAnswer( string username, string password, string newPasswordQuestion, string newPasswordAnswer)
33 {
34 throw new Exception( " The method or operation is not implemented. " );
35 }
36
37 public override MembershipUser CreateUser( string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
38 {
39 throw new Exception( " The method or operation is not implemented. " );
40 }
41
42 public override bool DeleteUser( string username, bool deleteAllRelatedData)
43 {
44 throw new Exception( " The method or operation is not implemented. " );
45 }
46
47 public override bool EnablePasswordReset
48 {
49 get { throw new Exception( " The method or operation is not implemented. " ); }
50 }
51
52 public override bool EnablePasswordRetrieval
53 {
54 get { throw new Exception( " The method or operation is not implemented. " ); }
55 }
56
57 public override MembershipUserCollection FindUsersByEmail( string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
58 {
59 throw new Exception( " The method or operation is not implemented. " );
60 }
61
62 public override MembershipUserCollection FindUsersByName( string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
63 {
64 throw new Exception( " The method or operation is not implemented. " );
65 }
66
67 public override MembershipUserCollection GetAllUsers( int pageIndex, int pageSize, out int totalRecords)
68 {
69 throw new Exception( " The method or operation is not implemented. " );
70 }
71
72 public override int GetNumberOfUsersOnline()
73 {
74 throw new Exception( " The method or operation is not implemented. " );
75 }
76
77 public override string GetPassword( string username, string answer)
78 {
79 throw new Exception( " The method or operation is not implemented. " );
80 }
81
82 public override MembershipUser GetUser( string username, bool userIsOnline)
83 {
84 return null ;
85 }
86
87 public override MembershipUser GetUser( object providerUserKey, bool userIsOnline)
88 {
89 throw new Exception( " The method or operation is not implemented. " );
90 }
91
92 public override string GetUserNameByEmail( string email)
93 {
94 throw new Exception( " The method or operation is not implemented. " );
95 }
96
97 public override int MaxInvalidPasswordAttempts
98 {
99 get { throw new Exception( " The method or operation is not implemented. " ); }
100 }
101
102 public override int MinRequiredNonAlphanumericCharacters
103 {
104 get { return 0 ; }
105 }
106
107 public override int MinRequiredPasswordLength
108 {
109 get { return minRequiredPasswordLength; }
110 }
111
112 public override int PasswordAttemptWindow
113 {
114 get { throw new Exception( " The method or operation is not implemented. " ); }
115 }
116
117 public override MembershipPasswordFormat PasswordFormat
118 {
119 get { throw new Exception( " The method or operation is not implemented. " ); }
120 }
121
122 public override string PasswordStrengthRegularExpression
123 {
124 get { throw new Exception( " The method or operation is not implemented. " ); }
125 }
126
127 public override bool RequiresQuestionAndAnswer
128 {
129 get { return requiresQuestionAndAnswer; }
130 }
131
132 public override bool RequiresUniqueEmail
133 {
134 get { throw new Exception( " The method or operation is not implemented. " ); }
135 }
136
137 public override string ResetPassword( string username, string answer)
138 {
139 throw new Exception( " The method or operation is not implemented. " );
140 }
141
142 public override bool UnlockUser( string userName)
143 {
144 throw new Exception( " The method or operation is not implemented. " );
145 }
146
147 public override void UpdateUser(MembershipUser user)
148 {
149 throw new Exception( " The method or operation is not implemented. " );
150 }
151
152 public override bool ValidateUser( string username, string password)
153 {
154 return true ;
155 }
156 }
2.在Web Application项目中,配置Web config 文件。
1
<
authentication
mode
="Forms"
/>
2
3 < membership defaultProvider ="CustomMemberShipProvider" >
4 < providers >
5 < clear />
6 < add name ="CustomMemberShipProvider"
7 type ="DataAcess.MemberShip.CustomMemberShipProvider"
8 connectionString ="XuConnectionString"
9 requiresQuestionAndAnswer ="true" />
10 </ providers >
11 </ membership >
2
3 < membership defaultProvider ="CustomMemberShipProvider" >
4 < providers >
5 < clear />
6 < add name ="CustomMemberShipProvider"
7 type ="DataAcess.MemberShip.CustomMemberShipProvider"
8 connectionString ="XuConnectionString"
9 requiresQuestionAndAnswer ="true" />
10 </ providers >
11 </ membership >
3.Login控件
创建default.aspx页面