日期:2009-04-18  浏览次数:20435 次

 

可以看出,Whidbey默认使用SqlMembershipProvider或者AccessMembershipProvider来进行用户验证和管理。这两个Provider实现了IProvider和IMembershipProvider接口,实际上这两个接口也是每个MembershipProvider所必需的,其中IProvider负责Provider的初始化,而IMembershipProvider则实现MembershipProvider的主要功能。它们的定义如下:



namespace System.Configuration.Provider
{
 public interface IProvider
 {
  public string Name { get; }
  public void Initialize(string name,
  System.Collections.Specialized.NameValueCollection config);
 }
}

namespace System.Web.Security
{
 public interface IMembershipProvider
 {
  public bool ChangePassword(string name, string oldPwd, string newPwd);
  public bool ChangePasswordQuestionAndAnswer(string name, string password,
string newPwdQuestion, string newPwdAnswer);
  public System.Web.Security.MembershipUser CreateUser(string username, string password, string email,out System.Web.Security.MembershipCreateStatus status);
  public bool DeleteUser(string name);
  public System.Web.Security.MembershipUserCollection GetAllUsers();
  public int GetNumberOfUsersOnline();
  public string GetPassword(string name, string answer);
  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属性的值是这样的字符串:

<membership>
 <providers>
  <add name="MyMembershipProvider" type="MyMembershipProvider" appName="/" />
 </providers>
</member