在ASP.NET Atlas中結合Membership進行身分識別驗證

來源:互聯網
上載者:User

作者:Dflying Chen (http://dflying.cnblogs.com/ )

ASP.NET Atlas可以使用ASP.NET中的Membership來進行使用者身分識別驗證,並在驗證成功後自動設定相應的Cookie。Atlas中的身分識別驗證是通過Sys.Services._AuthenticationService類的一個執行個體:Sys.Services.AuthenticationService來進行的,在Atlas應用程式中,您可以通過這個全域的Sys.Services.AuthenticationService對象來進新身分識別驗證。

Sys.Services.AuthenticationService對象有如下幾個方法:

  1. validateUser():該方法接受使用者名稱,密碼兩個參數,並將返回一個布爾值代表使用者驗證(注意,僅僅為驗證,不是登入,該方法將不會設定Cookie。)是否成功。該方法將使用ASP.NET中設定的預設的membership provider來進行使用者的驗證。
  2. login():這個方法與validateUser()方法類似,但在其基礎上該方法會設定代表登入成功的Cookie,當然需要在提供的使用者名稱/密碼正確的情況下。通過調用這個方法,您可以實現AJAX方式的使用者登入。
  3. logout():登出目前使用者。

下面我們通過一個例子來示範一下使用Sys.Services.AuthenticationService對象進行使用者身分識別驗證。

首先,在您的web.config檔案中啟用相應的驗證服務:

<configSections>
    <sectionGroup name="microsoft.web" type="Microsoft.Web.Configuration.MicrosoftWebSectionGroup">
        <section name="converters" type="Microsoft.Web.Configuration.ConvertersSection" requirePermission="false"/>
        <section name="webServices" type="Microsoft.Web.Configuration.WebServicesSection" requirePermission="false"/>
        <section name="authenticationService" type="Microsoft.Web.Configuration.AuthenticationServiceSection" requirePermission="false"/>
        <section name="profileService" type="Microsoft.Web.Configuration.ProfileServiceSection" requirePermission="false"/>
    </sectionGroup>
</configSections>

還有:

<microsoft.web>
    <webServices enableBrowserAccess="true"/>
    <!--
    Uncomment this line to enable the authentication service.-->
  <authenticationService enabled="true" />
</microsoft.web>

然後我們在Membership資料庫中添加幾個測試的使用者,您可以通過ASP.NET Web Site Administration Tool來設定並添加使用者。

現在我們建立一個簡單的登入頁面,與所有的登入頁面類似,兩個input(使用者名稱/密碼)一個按鈕(登入)。我們又加入了一個label來顯示使用者登入資訊。代碼如下:

Status: <span style="color: Red;" id="status">logged out</span><br />
User Name:<input type="text" id="username" /><br />
Password:<input type="password" id="password" /><br />
<input id="loginlogout" type="button" onclick="OnSubmitLogin()" value="Click me to login!" /><br />

 

當然,最重要的ScriptManager是不能缺少的:

<atlas:ScriptManager ID="scriptManager" runat="server" />

下面,我們來書寫登入按鈕按下時候的事件處理函數,也就是登入的處理。首先,利用Atlas的$()方法在DOM中找到上述幾個HTML控制項:

var username = $('username');
var password = $('password');
var status = $('status');
var buttonLoginLogout = $('loginlogout');

 

下面是使用者登入時的處理,注意到我們只是簡單的調用了Sys.Services.AuthenticationService.login()方法,並在返回以後相應改變狀態label的文字:

function OnSubmitLogin() {   
    Sys.Services.AuthenticationService.login(username.value, password.value, false, OnLoginComplete); 
    return false;
}
function OnLoginComplete(result) {
    password.value = '';

    //On success there will be a forms authentication cookie in the browser.
    if (result) {
        username.value = '';
        status.innerHTML = "logged in";
       
        buttonLoginLogout.innerText = "Click me to logout!";         
        buttonLoginLogout.onclick = OnSubmitLogout;
    }
    else {
        status.innerHTML = "User name/Password not match!";
    }
}

 

下面是使用者登出時的處理,通過調用Sys.Services.AuthenticationService.logout()方法來實現:

function OnSubmitLogout() {  
    //This call will cause the server to clear the forms authentication cookie. 
    Sys.Services.AuthenticationService.logout(OnLogoutComplete); 
    return false;

function OnLogoutComplete(result) {
    
    buttonLoginLogout.innerText = "Click me to login!"; 
    status.innerHTML = "logged out"; 
    buttonLoginLogout.onclick = OnSubmitLogin;
}  

 

以上執行個體程式可以在此下載:http://files.cnblogs.com/dflying/AuthenticationTest.zip

在下載的檔案中我添加了一個SecuredFolder檔案夾,其中內容只有登入後的使用者才能訪問,以便朋友們測試。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.