在ASP.NET Atlas中结合Membership进行身份验证

时间:2021-03-07 00:54:56

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

ASP.NET Atlas可以使用ASP.NET中的Membership来进行用户身份验证,并在验证成功后自动设定相应的CookieAtlas中的身份验证是通过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文件中启用相应的验证服务:

在ASP.NET Atlas中结合Membership进行身份验证< configSections >
在ASP.NET Atlas中结合Membership进行身份验证    
< sectionGroup  name ="microsoft.web"  type ="Microsoft.Web.Configuration.MicrosoftWebSectionGroup" >
在ASP.NET Atlas中结合Membership进行身份验证        
< section  name ="converters"  type ="Microsoft.Web.Configuration.ConvertersSection"  requirePermission ="false" />
在ASP.NET Atlas中结合Membership进行身份验证        
< section  name ="webServices"  type ="Microsoft.Web.Configuration.WebServicesSection"  requirePermission ="false" />
在ASP.NET Atlas中结合Membership进行身份验证        
< section  name ="authenticationService"  type ="Microsoft.Web.Configuration.AuthenticationServiceSection"  requirePermission ="false" />
在ASP.NET Atlas中结合Membership进行身份验证        
< section  name ="profileService"  type ="Microsoft.Web.Configuration.ProfileServiceSection"  requirePermission ="false" />
在ASP.NET Atlas中结合Membership进行身份验证    
</ sectionGroup >
在ASP.NET Atlas中结合Membership进行身份验证
</ configSections >

还有:

在ASP.NET Atlas中结合Membership进行身份验证< microsoft .web >
在ASP.NET Atlas中结合Membership进行身份验证    
< webServices  enableBrowserAccess ="true" />
在ASP.NET Atlas中结合Membership进行身份验证    
<!--
在ASP.NET Atlas中结合Membership进行身份验证    Uncomment this line to enable the authentication service.
-->
在ASP.NET Atlas中结合Membership进行身份验证  
< authenticationService  enabled ="true"   />
在ASP.NET Atlas中结合Membership进行身份验证
</ microsoft.web >

然后我们在Membership数据库中添加几个测试的用户,您可以通过ASP.NET Web Site Administration Tool来设置并添加用户。

现在我们创建一个简单的登录页面,与所有的登录页面类似,两个input(用户名/密码)一个按钮(登录)。我们又加入了一个label来显示用户登录信息。代码如下:

在ASP.NET Atlas中结合Membership进行身份验证Status:  < span  style ="color: Red;"  id ="status" > logged out </ span >< br  />
在ASP.NET Atlas中结合Membership进行身份验证User Name:
< input  type ="text"  id ="username"   />< br  />
在ASP.NET Atlas中结合Membership进行身份验证Password:
< input  type ="password"  id ="password"   />< br  />
在ASP.NET Atlas中结合Membership进行身份验证
< input  id ="loginlogout"  type ="button"  onclick ="OnSubmitLogin()"  value ="Click me to login!"   />< br  />

 

当然,最重要的ScriptManager 是不能缺少的:
在ASP.NET Atlas中结合Membership进行身份验证< atlas:ScriptManager  ID ="scriptManager"  runat ="server"   />

下面,我们来书写登录按钮按下时候的事件处理函数,也就是登录的处理。首先,利用Atlas$()方法在DOM中找到上述几个HTML控件:

在ASP.NET Atlas中结合Membership进行身份验证var  username  =  $('username');
在ASP.NET Atlas中结合Membership进行身份验证
var  password  =  $('password');
在ASP.NET Atlas中结合Membership进行身份验证
var  status  =  $('status');
在ASP.NET Atlas中结合Membership进行身份验证
var  buttonLoginLogout  =  $('loginlogout');

 

下面是用户登录时的处理,注意到我们只是简单的调用了Sys.Services.AuthenticationService.login()方法,并在返回以后相应改变状态label的文字:

在ASP.NET Atlas中结合Membership进行身份验证在ASP.NET Atlas中结合Membership进行身份验证function  OnSubmitLogin()  {   
在ASP.NET Atlas中结合Membership进行身份验证    Sys.Services.AuthenticationService.login(username.value, password.value, 
false, OnLoginComplete); 
在ASP.NET Atlas中结合Membership进行身份验证    
return false;
在ASP.NET Atlas中结合Membership进行身份验证}

在ASP.NET Atlas中结合Membership进行身份验证在ASP.NET Atlas中结合Membership进行身份验证
function  OnLoginComplete(result)  {
在ASP.NET Atlas中结合Membership进行身份验证    password.value 
= '';
在ASP.NET Atlas中结合Membership进行身份验证
在ASP.NET Atlas中结合Membership进行身份验证    
//On success there will be a forms authentication cookie in the browser.
在ASP.NET Atlas中结合Membership进行身份验证在ASP.NET Atlas中结合Membership进行身份验证
    if (result) {
在ASP.NET Atlas中结合Membership进行身份验证        username.value 
= '';
在ASP.NET Atlas中结合Membership进行身份验证        status.innerHTML 
= "logged in";
在ASP.NET Atlas中结合Membership进行身份验证       
在ASP.NET Atlas中结合Membership进行身份验证        buttonLoginLogout.innerText 
= "Click me to logout!";         
在ASP.NET Atlas中结合Membership进行身份验证        buttonLoginLogout.onclick 
= OnSubmitLogout;
在ASP.NET Atlas中结合Membership进行身份验证    }

在ASP.NET Atlas中结合Membership进行身份验证在ASP.NET Atlas中结合Membership进行身份验证    
else {
在ASP.NET Atlas中结合Membership进行身份验证        status.innerHTML 
= "User name/Password not match!";
在ASP.NET Atlas中结合Membership进行身份验证    }

在ASP.NET Atlas中结合Membership进行身份验证}

 

下面是用户注销时的处理,通过调用Sys.Services.AuthenticationService.logout()方法来实现:

在ASP.NET Atlas中结合Membership进行身份验证在ASP.NET Atlas中结合Membership进行身份验证function  OnSubmitLogout()  {  
在ASP.NET Atlas中结合Membership进行身份验证    
//This call will cause the server to clear the forms authentication cookie. 
在ASP.NET Atlas中结合Membership进行身份验证
    Sys.Services.AuthenticationService.logout(OnLogoutComplete); 
在ASP.NET Atlas中结合Membership进行身份验证    
return false;
在ASP.NET Atlas中结合Membership进行身份验证}
 
在ASP.NET Atlas中结合Membership进行身份验证在ASP.NET Atlas中结合Membership进行身份验证
function  OnLogoutComplete(result)  {
在ASP.NET Atlas中结合Membership进行身份验证    
在ASP.NET Atlas中结合Membership进行身份验证    buttonLoginLogout.innerText 
= "Click me to login!"
在ASP.NET Atlas中结合Membership进行身份验证    status.innerHTML 
= "logged out"
在ASP.NET Atlas中结合Membership进行身份验证    buttonLoginLogout.onclick 
= OnSubmitLogin;
在ASP.NET Atlas中结合Membership进行身份验证}
  

 

以上实例程序可以在此下载:http://files.cnblogs.com/dflying/AuthenticationTest.zip

在下载的文件中我添加了一个SecuredFolder文件夹,其中内容只有登录后的用户才能访问,以便朋友们测试。