ASP.NET 常式完全代碼版(7)——2.0中實現自配置的成員角色管理庫

來源:互聯網
上載者:User
ASP.NET 常式完全代碼版(7)——2.0中實現自配置的成員角色管理庫
作者: zyf0808 發表日期: 2006-05-12 10:01 文章屬性: 原創 複製連結

    在ASP.NET 2.0 中,為我們提供了很好的成員即角色管理機制,可以方便地實現使用者的註冊及角色指派,對於基本的成員訪問和角色控制進行了封裝實現,但是,預設的情況下,這個用於提供成員即角色管理的資料庫 aspnetdb 是要儲存在 SQL Server 2005 的 Express 版本中的,下面通過建立個人網站的解決方案來講述如何通過組態工具,實現 VS 2005 與 SQL Server 2000 的整合開發。
    首先,安裝好 VS 2005 後,如果 File-->New-->Web Site-->Personal Web Site Starter Kit 然後直接 RUN 的話,肯定會出現錯誤,提示 An error has occurred while establishing a connection to the server.  When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) 。但是,VS 給我們提供了很方便的使用工具,可以在命令列下使用 aspnet_regsql.exe 來指定預設使用的資料庫伺服器。它會為我們自動在配置的資料庫伺服器下產生 aspnetdb 這個用來管理成員即角色的庫,這個工具可以在命令列下使用,也提供了 UI ,可以方便配置應用程式使用的資料庫。aspnet_regsql

 
    在使用這個 Starter Kits 的時候,可以把 Personal 的庫通過它提供的指令碼,儲存在 SQL Server 2000 中,而不用使用 AttachDBFilename 的方式。具體可以通過 web.config 來配置,如下:
<connectionStrings>
    <add name="Personal" connectionString="Data Source=yourServer;Integrated Security=True;Initial Catalog=Personal" />
    <add name="SqlServices" connectionString="Data Source=yourServer;Integrated Security=SSPI;Initial Catalog=aspnetdb;" />
    <remove name="LocalSqlServer"/>
</connectionStrings>
    預設情況下,使用的串連是 machine.config 中定義的 LocalSqlServer ,你可以 remove 掉,同時,也不使用預設的 Membership & roles 的 Provider,也需要將 machine.config 中的預設 Provider 去掉。因為在其中,有如下的配置:
 <roleManager>
    <providers>
       <add name="AspNetSqlRoleProvider" connectionStringName="LocalSqlServer" applicationName="/" type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </providers>
</roleManager>
因為我們已經 remove 掉了預設的 LocalSqlServer 連接字串,如果不 remove 掉 AspNetSqlRoleProvider 的話,會提示 The connection name 'LocalSqlServer' was not found in the applications configuration or the connection string is empty.
具體的使用自定製的方式可以如下設定:
<system.web>
    <roleManager enabled="true" defaultProvider ="AspNetRoleProvider">
      <providers>
        <remove name="AspNetSqlRoleProvider" />
        <add name="AspNetRoleProvider" connectionStringName="SqlServices"
             type="System.Web.Security.SqlRoleProvider,System.Web, Version=2.0.0.0, Culture=neutral,    PublicKeyToken=b03f5f7f11d50a3a"
            applicationName="/" />
      </providers>
    </roleManager>
    <membership defaultProvider="AspNetMembershipProvider" userIsOnlineTimeWindow="20">
      <providers>
        <remove name="AspNetSqlMembershipProvider" />
        <add name="AspNetMembershipProvider"
            type="System.Web.Security.SqlMembershipProvider"
            connectionStringName="SqlServices"
            enablePasswordRetrieval="false"
            enablePasswordReset="true"
            requiresQuestionAndAnswer="true"
            passwordFormat="Hashed"
            applicationName="/" />
      </providers>
    </membership>
</system.web>
   經過如此一番配置,你就可以使用 VS 2005 & SQL Server 2000 來實現成員訪問和角色管理的開發了。呵呵,希望對剛介入 2.0 開發的朋友有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.