ASP.NET Whidbey中实现Provider__教程 |
|
日期:2007-5-20 1:16:22 人气:46 [大 中 小] |
|
|
|
public System.Web.Security.MembershipUser GetUser(string name,bool userIsOnline); public string GetUserNameByEmail(string email); public string ResetPassword(string name, string answer); public void UpdateUser(System.Web.Security.MembershipUser user); public bool ValidateUser(string name, string password); public string ApplicationName {get; set;} public bool EnablePasswordReset { get;} public bool EnablePasswordRetrieval { get;} public bool RequiresQuestionAndAnswer { get;} } }
现在可以动手来实现我们自己的MembershipProvider了:
public class MyMembershipProvider : IProvider, IMembershipProvider { …… }
验证功能是必需的:
public bool ValidateUser (string name, string password) { string connectStr = ConfigurationSettings.ConnectionStrings["PortalData"]; SqlConnection myConnection = new SqlConnection (connectStr); SqlCommand myCommand = new SqlCommand ("UserLogin", myConnection); myCommand.CommandType = CommandType.StoredProcedure;
// Add Parameters to SPROC SqlParameter parameterEmail = new SqlParameter ("@Email", SqlDbType.NVarChar, 100); parameterEmail.Value = name; myCommand.Parameters.Add (parameterEmail);
SqlParameter parameterPassword = new SqlParameter ("@Password", SqlDbType.NVarChar, 20); parameterPassword.Value = password; myCommand.Parameters.Add (parameterPassword);
SqlParameter parameterUserName = new SqlParameter ("@UserName", SqlDbType.NVarChar, 100); parameterUserName.Direction = ParameterDirection.Output; myCommand.Parameters.Add (parameterUserName);
// Open the database connection and execute the command myConnection.Open (); myCommand.ExecuteNonQuery (); myConnection.Close (); if ((parameterUserName.Value != null) && (parameterUserName.Value != System.DBNull.Value)) return true; return false; }
现在在web.config中可以这样配置connectionString了:
<connectionStrings>
<add name="BugDepotData" connectionString="Data Source=(local);Trusted_Connection=true;Database=Portal" />
</connectionStrings>
这样,我们自己的一个简单的MembershipProvider就基本上完成了。接下来需要配置web.config,让需要Provider服务的控件能够认识它:
<membership> <providers> <add name="MyMembershipProvider" type="MyMembershipProvider" appName="/" /> </providers> </membership>
这段设置是参考machine.config而来的,其中type属性的值是这样的字符串:
type="ProviderType, Assembly, Version, Culture, PublicKeyToken"
由于我们的MyMembershipProvider放在/Code目录下,并不是在单独的Assembly中,因此只需要指出ProviderType就行了。
这样,一个具有验证功能的Provider就完成了,现在可以在页面上放一个新的Security控件,比如Login控件,并指定它的MembershipProperty为MyMembershipProvider(或者也可以设置membership的defaultProvider属性为MyMembershipProvider),打开Forms验证,试试是不是已经能够成功登陆了? |
|
出处:本站原创 作者:佚名 |
|
|