1. 程式人生 > >在SQL Sever中使用form membership認證

在SQL Sever中使用form membership認證

visual studio 2005 提供了非常方面的通過 asp.net configuration設定使用者以及驗證的方式。同時提供了登入控制元件來操作登入。不過讓我難過的是,預設是採用sql express在你的app_data建立一個mdb檔案。我現在要使用sql server來儲存那麼我需要一些配置。

1. 首先到你安裝.net frame works框架的目錄輸入下面命令,安裝membership到你的SQL server

C:/Documents and Settings/xxpp>C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/aspnet_regsql.exe -S (local) -E -A m

Start adding the following features:
Membership

..引數-S 表明你是哪個sql server,我目前是本機,所以採用local,其他的自己換名字了。

引數-E 連結SQL 採用windwos的方式,本機不用輸入密碼呢。

引數-A m 增加membership 的feature 

成功完成後,開啟你的SQL SERVER 2000你就會看到已經多了一個aspnetdb的資料

2.隨後我們在web.config裡面新增以下資料庫連線,就是連結到剛才新建的資料庫

 其中呢,Data Source =xxx,表示我的資料來源,我採用混合認證方式,User ID=sa Password=Pass,我我自己的使用者名稱密碼呢。

<connectionStrings>
  
<add name="aspnetdbConnectionString" connectionString="Data Source=xxx;Initial Catalog=aspnetdb;Persist Security Info=True;User ID=sa;Password=Pass"
   providerName
="System.Data.SqlClient"/>
 
</connectionStrings>

3.配置站點開啟web.config新增以下MebmbershipProvide

其中要注意的就是connectionStringName="aspnetdbConnectionString"這個一定要同你在2步驟中配置的名一樣。

我們也可以看到我們添加了一個MySqlMembershipProvider

<membership defaultProvider="MySqlMembershipProvider"><providers><clear/><add name="MySqlMembershipProvider"
             connectionStringName
="aspnetdbConnectionString"
             applicationName
="MyAppName"
             type
="System.Web.Security.SqlMembershipProvider, 
                        System.Web, Version=2.0.0.0, Culture=neutral, 
                        PublicKeyToken=b03f5f7f11d50a3a"
/></providers></membership>

4.ok,在visual studio 2005 裡面工具欄,點選WebSite -ASP.NET Configuration。看到關於web配置頁面。

在裡面選擇Provider ,然後點選其中的“Select a different provider for each feature(advanced)

ok ,已經可以看到在Membership Provide的列表裡面看到了MySqlMembershipProvider了,點選Test,成功連結。

 5.另外一種也可以的辦法如下,修改LocalSqlServer配置。我們可以看到在C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/CONFIG/machine.config裡面有這個選項。不過我們不需要修改。只是在我們de webconfig裡面過載一個呢。以下是另外一種web.config重點就是connectionstring 裡面的東西

<?xml version="1.0"?>
<!-- 
    Note: As an alternative to hand editing this file you can use the 
    web admin tool to configure settings for your application. Use
    the Website->Asp.Net Configuration option in Visual Studio.
    A full list of settings and comments can be found in 
    machine.config.comments usually located in 
    WindowsMicrosoft.NetFrameworkv2.xConfig 
-->
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
    
<appSettings/>
    
<connectionStrings>
    
<remove name="LocalSqlServer"/>
    
<add name="LocalSqlServer" connectionString="Data Source=XPXPCOM;Initial Catalog=aspnetdb;Integrated Security=True"
     providerName
="System.Data.SqlClient"/>

    
<add name="aspnetdbConnectionString" connectionString="Data Source=XXXXX;Initial Catalog=aspnetdb;Persist Security Info=True;User ID=sa;Password=Pass"
   providerName
="System.Data.SqlClient"/>
 
</connectionStrings>
    
<system.web>
        
<!-- 
            Set compilation debug="true" to insert debugging 
            symbols into the compiled page. Because this 
            affects performance, set this value to true only 
            during development.
        
-->
        
<compilation debug="true"/>
        
<!--
            The <authentication> section enables configuration 
            of the security authentication mode used by 
            ASP.NET to identify an incoming user. 
        
-->
    
<!--
        <authentication mode="Windows"/>
    
-->
    
<authentication mode="Forms"/>
        
<!--
            The <customErrors> section enables configuration 
            of what to do if/when an unhandled error occurs 
            during the execution of a request. Specifically, 
            it enables developers to configure html error pages 
            to be displayed in place of a error stack trace.

        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            <error statusCode="403" redirect="NoAccess.htm" />
            <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>
        
-->
  


  
</system.web>
</configuration>