[ASPNET2.0]Membership類+SQLServer2005,AspNet_regsql.exe的使用

成員關係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”
點測試完成連接測試。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章