[導入]AjaxPro新發現(一):AuthenticationService
使用AjaxPro有一段時日了,感覺是AjaxPro功能雖然沒有Asp.net Ajax那么強大,但是很夠用,大家關注這個框架大多終止在Asp.net 1.x .可是我認識這個框架的時候,最新版本已經能很好的支持.Net2.0的一些新的特性,比如AuthenticationService(Membership),ProfileService.在這個系列中,我會總結過去一段時間的一些新的發現,包括服務器和客戶端的,這里我需要告訴大家,AjaxPro 的客戶端也是值得一說的.
我這里介紹的內容都是建立在你對AjaxPro 有一定的了解的基礎上的,多一句嘴
Authentication Service
成員資格管理是.Net2.0中的一個新特性,它提供了基于角色的驗證和訪問控制,在客戶端是通過Cookie存儲用戶的信息,也有使用Cookiless,AjaxPro 只能在Cookie驗證的情況下才能使用
首先下載支持.Net2.0的AjaxPro框架,在web.config里注冊HttpHandler就OK了
<!--Http handler for AjaxPro-->
<httpHandlers>
<add verb="*" path="*.ashx" type="AjaxPro.AjaxHandlerFactory,AjaxPro.2"/>
</httpHandlers>
頁面里注冊AjaxPro.Services.AuthenticationService:
<httpHandlers>
<add verb="*" path="*.ashx" type="AjaxPro.AjaxHandlerFactory,AjaxPro.2"/>
</httpHandlers>
//AjaxProAuthenticationService.aspx.cs
public partial class AjaxProServices : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
AjaxPro.Utility.RegisterTypeForAjax(typeof(AjaxPro.Services.AuthenticationService));
AjaxPro.Utility.RegisterTypeForAjax(typeof(AjaxProServices));
}
}
客戶端能使用的方法有以下三個:
public partial class AjaxProServices : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
AjaxPro.Utility.RegisterTypeForAjax(typeof(AjaxPro.Services.AuthenticationService));
AjaxPro.Utility.RegisterTypeForAjax(typeof(AjaxProServices));
}
}
- AjaxPro.Services.Authentication.Login(UserName,Password,callback):callback回調函數參數result.value為bool型,驗證成功為true,否則為false .
- AjaxPro.Services.Authentication.ValidUser(callback):檢查是否通過身份,callback參數result.value返回true/false.
- AjaxPro.Services.Authentication.Logout();用戶注銷,可以不需要回調函數
基于方法的權限控制
在支持.Net2.0 的AjaxPro版本中可以控制客戶端方法的訪問權限.Michael Schwarz在他的文檔里提供了這么一個方法
[AjaxPro.AjaxMethod]
[PrincipalPermission(SecurityAction.Demand, Role = "Admin")]
[PrincipalPermission(SecurityAction.Deny, Name="?")]
public DateTime GetServerTime()
{
return DateTime.Now;
}
使用PrincipalPermissionAttribute可用于以聲明方式要求運行您的代碼的用戶屬于指定的角色或者已經過身份驗證,上面的方法允許角色Admin訪問同時拒絕匿名用戶訪問,這個方法不限于AjaxPro ,可以在.Net2.0環境中使用.其標記屬性包括
[PrincipalPermission(SecurityAction.Demand, Role = "Admin")]
[PrincipalPermission(SecurityAction.Deny, Name="?")]
public DateTime GetServerTime()
{
return DateTime.Now;
}
- Action:SecurityAction枚舉型,值可以為Assert, Demand, InheritanceDemand, LinkDemand, PermitOnly, RequestMinimum, RequestOptional, RequestRefuse.
- Role:允許訪問或者拒絕的角色名
- Name:針對某用戶的權限控制,*表示全部,?表示匿名.
- Authenticated:true/false,是否要求身份驗證
- Unrestricted:true/false不限制訪問
與Asp.net Ajax 比較起來,AjaxPro的使用比較簡單,功能也大相徑庭,有AuthenticationService自然會有ProfileService,下篇文章介紹
--------------------------
盛大招聘.Net開發工程師
經典好書:.NET框架程序設計(修訂版)
新聞:Windows 7 Beta 1 可以下載了
導航:博客園首頁 知識庫 新聞 招聘 社區 小組 博問 網摘 找找看
文章來源:http://www.cnblogs.com/Hafeyang/archive/2007/10/16/AjaxProAuthentication.html
posted on 2007-10-16 20:12 衡鋒 閱讀(269) 評論(0) 編輯 收藏 所屬分類: AjaxPro