ASP.NET Whidbey中实现Provider(2)
发表于:2007-06-30来源:作者:点击数:
标签:
可以看出,Whidbey默认使用SqlMembershipProvider或者AccessMembershipProvider来进行用户验证和管理。这两个Provider实现了IProvider和IMembershipProvider接口,实际上这两个接口也是每个MembershipProvider所必需的,其中IProvider负责Provider的初始化,
可以看出,Whidbey默认使用SqlMembershipProvider或者A
clearcase/" target="_blank" >ccessMembershipProvider来进行用户验证和管理。这两个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属性的值是这样的字符串:
type="ProviderType, Assembly, Version, Culture, PublicKeyToken"
由于我们的MyMembershipProvider放在/Code目录下,并不是在单独的Assembly中,因此只需要指出ProviderType就行了。
这样,一个具有验证功能的Provider就完成了,现在可以在页面上放一个新的Security控件,比如Login控件,并指定它的MembershipProperty为MyMembershipProvider(或者也可以设置membership的defaultProvider属性为MyMembershipProvider),打开Forms验证,试试是不是已经能够成功登陆了?
原文转自:http://www.ltesting.net