“whidbey”是微軟工具套件的下一個版本。按照微軟的計劃,它將會在2004年底推出。
asp.net 2.0(codename whidbey)通過provider模式為用戶驗證、角色管理等方面提供了非常強(qiáng)大易用的框架模型。whidbey中提供了一個asp.net configuration工具,通過它可以非常容易地配置用戶信息數(shù)據(jù)庫,管理角色等等,再與新加入的security控件配合,幾乎不用寫什么代碼就能夠?qū)崿F(xiàn)用戶驗證和角色管理功能。關(guān)于這些控件和配置工具的具體使用,可以參考這篇文章:使用更精簡的代碼保證 asp.net 應(yīng)用程序的安全
但是在pdc preview版本的whidbey中,這個配置工具的功能還不是很完善。從我使用的情況來看,它目前還只能創(chuàng)建和連接自己的demo用的access數(shù)據(jù)庫,不能連接sql server數(shù)據(jù)庫進(jìn)行擴(kuò)展。因此,為了能夠連接sql server,我們必須提供我們自己的providers。這里以連接ibuyspy的portal數(shù)據(jù)庫為例來說明如何實現(xiàn)一個membership provider。
為了搞清楚如何實現(xiàn)我們自己的membership provider,有必要先看看whidbey默認(rèn)使用的membership provider是如何做的。在machine.config配置文件中,whidbey使用類似下面這樣的配置實現(xiàn):
<membership defaultprovider="aspnetaccessprovider" userisonlinetimewindow="15" >
<providers>
<add name="aspnetsqlprovider"
type="system.web.security.sqlmembershipprovider, system.web, version=1.2.3400.0, culture=neutral, publickeytoken=b03f5f7f11d50a3a" connectionstringname="localsqlserver"
enablepasswordretrieval="false"
enablepasswordreset="true"
requiresquestionandanswer="false"
applicationname="/"
requiresuniqueemail="false"
passwordformat="hashed"
description="stores and retrieves membership data from the local microsoft sql server database"
/>
<add name="aspnetaccessprovider"
type="system.web.security.accessmembershipprovider, system.web, version=1.2.3400.0, culture=neutral, publickeytoken=b03f5f7f11d50a3a"
connectionstringname="accessfilename"
enablepasswordretrieval="false"
enablepasswordreset="true"
requiresquestionandanswer="false"
applicationname="/"
requiresuniqueemail="false"
passwordformat="hashed"
description="stores and retrieves membership data from the local microsoft access database file"
/>
</providers>
</membership>
關(guān)于這段配置文件的更詳細(xì)解說,可以參考《a first look at asp.net v. 2.0》。
可以看出,whidbey默認(rèn)使用sqlmembershipprovider或者accessmembershipprovider來進(jìn)行用戶驗證和管理。這兩個provider實現(xiàn)了iprovider和imembershipprovider接口,實際上這兩個接口也是每個membershipprovider所必需的,其中iprovider負(fù)責(zé)provider的初始化,而imembershipprovider則實現(xiàn)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;}
}
}
現(xiàn)在可以動手來實現(xiàn)我們自己的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;
}
現(xiàn)在在web.config中可以這樣配置connectionstring了:
<connectionstrings>
<add name="bugdepotdata" connectionstring="data source=(local);trusted_connection=true;database=portal" />
</connectionstrings>
這樣,我們自己的一個簡單的membershipprovider就基本上完成了。接下來需要配置web.config,讓需要provider服務(wù)的控件能夠認(rèn)識它:
<membership>
<providers>
<add name="mymembershipprovider" type="mymembershipprovider" appname="/" />
?。?providers>
</membership>
這段設(shè)置是參考machine.config而來的,其中type屬性的值是這樣的字符串:
type="providertype, assembly, version, culture, publickeytoken"
由于我們的mymembershipprovider放在/code目錄下,并不是在單獨的assembly中,因此只需要指出providertype就行了。
這樣,一個具有驗證功能的provider就完成了,現(xiàn)在可以在頁面上放一個新的security控件,比如login控件,并指定它的membershipproperty為mymembershipprovider(或者也可以設(shè)置membership的defaultprovider屬性為mymembershipprovider),打開forms驗證,試試是不是已經(jīng)能夠成功登陸了?
國內(nèi)最大的酷站演示中心!