转自:http://www.cnblogs.com/atomplus/archive/2009/08/25/1553479.html
成员关系Membership类总是被默认地和SQLServerExpress联系起来使用,但你的数据库可能是其他的数据源提供的,比 如:SQLServer2000/2005,Oralce,OleDB,ODBC+Access,那么你可能需要做一些额外的工作来完成数据库和应用程序 之间的联系。
在MSDN中你可以很方便地查询到我们需要使用AspNet_regsql.exe工具来完成这个配置的过程。下面就以SQLServer2005的配置过程为例,图示一下:
一、打开aspnet_regsql.exe,单独使用以下语句将启动配置向导,你也可以利用命令行参数来完成配置过程。
v2.0.50727,可以在C:\WINDOWS\Microsoft.NET\Framework\下输入dir来获取.NET的版本号
MSDN关键字:Aspnet_regsql.exe可以查找相关参数配置的信息。
二、向导模式(在数据库中添加应用程序服务(成员资格、配置文件、角色管理、个性化设置(WebPart一类的)以及SQL Web事件提供程序)如果只需要单独添加其中的一项,请使用参数配置的方式,以下方式的默认参数为all)
三、查看刚才选中的数据库,图中以"aspnet_"开头的都是为了这个配置所生成的表,请不要试图添加任何的字段,因为与此搭配的还有一大堆的存储过程,如果修改了表结构,那么将会有意想不到的错误。
四、在应用程序中访问,我们还需要做一些配置。
(1)、打开Web.config文件,确保已经有存在connnectString配置节
(2)、在<system.web>和</system.web>之间添加以下配置:
<!--添加成员管理-->
<membership defaultProvider="SqlProvider" userIsOnlineTimeWindow="20">
<providers>
<remove name="AspNetSqlProvider" />
<add connectionStringName="WebAppBlogConnectionString" enablePasswordRetrieval="false"
enablePasswordReset="true" requiresQuestionAndAnswer="true"
passwordFormat="Hashed" applicationName="/" name="SqlProvider"
type="System.Web.Security.SqlMembershipProvider" />
</providers>
</membership>
<!--添加角色管理-->
<roleManager defaultProvider="SqlProvider"
enabled="true"
cacheRolesInCookie="true"
cookieName=".ASPROLES"
cookieTimeout="30"
cookiePath="/"
cookieRequireSSL="true"
cookieSlidingExpiration="true"
cookieProtection="All" >
<providers>
<add
name="SqlProvider"
type="System.Web.Security.SqlRoleProvider"
connectionStringName="WebAppBlogConnectionString"
applicationName="SampleApplication" />
</providers>
</roleManager>
其中的connectionStringName属性值都是指连接字符串的名字:
连接字符串类似:
<connectionStrings>
<add name="WebAppBlogConnectionString" connectionString="Data Source=.;Initial Catalog=WebAppBlog;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>