在ASP.NET Atlas中结合Membership进行身份验证
作者: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对象有如下几个方法:
- validateUser():该方法接受用户名,密码两个参数,并将返回一个布尔值代表用户验证(注意,仅仅为验证,不是登录,该方法将不会设置Cookie。)是否成功。该方法将使用ASP.NET中设置的默认的membership provider来进行用户的验证。
 - login():这个方法与validateUser()方法类似,但在其基础上该方法会设置代表登录成功的Cookie,当然需要在提供的用户名/密码正确的情况下。通过调用这个方法,您可以实现AJAX方式的用户登录。
 - 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;
}  
以上实例程序可以在此下载:https://files.cnblogs.com/dflying/AuthenticationTest.zip
在下载的文件中我添加了一个SecuredFolder文件夹,其中内容只有登录后的用户才能访问,以便朋友们测试。
                    
                



                
            
        
浙公网安备 33010602011771号