博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

转自: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>

 

只有完成了以上配置后才能再ASP.Net Web应用程序管理中找到相应的连接选项。
否则将默认连接SQLServerExpress的AspNetSqlProvider提供程序,而以上配置中有句<remove name="AspNetSqlProvider" />就是为了把默认的移除。

五、点Web项目,项目-〉ASP.NET配置,事实上这个配置web程序就是为了配置以上这段配置节所提供的。
将启动IE,在“提供程序”选项卡中选择“为每项功能选择不同的提供程序(高级)”。


 

确保成员资格提供程序和角色提供程序都是刚才你在配置节中指定的“SqlProvider”点测试完成连接测试。