标签:blog   http   java   使用   strong   数据   
备用收藏,http://blog.csdn.net/msdnxgh/article/details/6894885
 
.NET 实现自定义ContextUser的Identity和Principal 
在传统的.NET中,我们可以通过
 
- User.Identity.Name;
-   
- User.Identity.IsAuthenticated;
-   
- User.IsInRole("Admin");
 
 
但这样的机制,在实际开发中,难以满足开发需要.仅仅通过User.Identity.Name;获取用户名,和User.Identity.IsAuthenticated;判断用户是否验证,是难以满足需要。如何获取用户更多信息,或者进行更详细的权限判断。
 
我们可以通过自定义Identity和Principal进行实现!
 
 
 
好,上面我们己实现了自定义,Identity和Principal。
 
我们可以在页面这样使用Identity。
 
-   
- <%=(User.Identity as ContextUser.MyIdentity).Name %>
-   
- <%=(User.Identity as ContextUser.MyIdentity).Phone %>
-   
- <%=(User.Identity as ContextUser.MyIdentity).Departmnet %>
 
 
自定义显示用户信息后,我们接着利用Principal进行权限验证和控制
 
在Asp.net Web模式下,使用方式:
 
首先,我们先做一个权限验证基类!
-   
-   
-   
- public class BasePaper:System.Web.UI.Page  
-   
- {  
-   
-     public BasePaper()  
-   
-        {  
-   
-               
-   
-               
-   
-               
-   
-        }  
-   
-     protected override void OnInit(EventArgs e)  
-   
-     {  
-   
-         BasePage_Load();  
-   
-     }  
-   
-     
-   
-     
-   
-     
-   
-     public virtual int PermissionID  
-   
-     {  
-   
-         get { return 0; }  
-   
-     }  
-   
-     
-   
-     
-   
-     
-   
-     
-   
-     
-   
-     private void BasePage_Load()  
-   
-     {  
-   
-         
-  
-         #region 权限检查  
-   
-         bool Permission = true;
-   
-    
-   
-                 
-   
-                 ContextUser.MyPrincipal MyPrincipal = new ContextUser.MyPrincipal(HttpContext.Current.User.Identity.Name);  
-   
-                 HttpContext.Current.User = MyPrincipal;  
-   
-         if ((User as account.ContextUser.MyPrincipal).PermissionList.Contains(PermissionID))  
-   
-         {  
-   
-             Permission = false;
-   
-         }  
-   
-         if (Permission)
-   
-         {  
-   
-             Response.Clear();  
-   
-             Response.Write("<script language=\"javascript\">alert(\"对不起,你没有权限进入\");history.go(-1);</script>");  
-   
-             Response.End();  
-   
-         }  
-  
-         #endregion  
-   
-     }  
-   
- }  
 
 
OK,到了验证页的时候了。
 
- public partial class ascx_Add :BasePage  
-   
- {  
-   
-     public override int PermissionID  
-   
-     {  
-   
-         get  
-   
-         {  
-   
-             return 13;
-   
-         }  
-   
-     }  
-   
-     protected void Page_Load(object sender, EventArgs e)  
-   
-     {  
-   
-         
-   
-     }  
-   
- }  
 
 
 
事实上,在Asp.net MVC模式,更容易对权限进行控制,可以进行更多的细化,对每个动作进行控制。
 
MVC模式下:
 
首先,先实现一个权限验证基类:
 
-   
-   
-   
-   
- public class BasePage : AuthorizeAttribute  
-   
- {  
-   
-     
-   
-     
-   
-     
-   
-     private int _permissionID = 0;  
-   
-     
-   
-     
-   
-     
-   
-     public int PermissionID  
-   
-     {  
-   
-         get { return _permissionID; }  
-   
-         set { _permissionID = value; }  
-   
-     }  
-   
-     
-   
-     
-   
-     
-   
-     
-   
-     public override void OnAuthorization(AuthorizationContext filterContext)  
-   
-     {  
-   
-         if (HttpContext.Current.User.Identity.IsAuthenticated)  
-   
-         {  
-   
-             
-   
-             ContextUser.MyPrincipal MyPrincipal = new ContextUser.MyPrincipal(HttpContext.Current.User.Identity.Name);  
-   
-             HttpContext.Current.User = MyPrincipal;  
-   
-   
-   
-             if ((!MyPrincipal.ISPermissionID(_permissionID)) && (_permissionID != 0))  
-   
-             {  
-   
-                 HttpContext.Current.Response.Clear();  
-   
-                 HttpContext.Current.Response.Write("<script defer>window.alert(‘无权操作!‘);history.back();</script>");  
-   
-                 HttpContext.Current.Response.End();  
-   
-                 filterContext.Result = new EmptyResult();  
-   
-             }  
-   
-         }  
-   
-         else  
-   
-         {  
-   
-             FormsAuthentication.SignOut();  
-   
-             HttpContext.Current.Response.Clear();  
-   
-             HttpContext.Current.Response.Write("<script defer>window.alert(‘无权操作!或当前登录用户已过期!\\n请重新登录或与管理员联系!‘);</script>");  
-   
-             HttpContext.Current.Response.End();  
-   
-             filterContext.Result = new EmptyResult();  
-   
-         }  
-   
-     }  
-   
-   
-   
- }  
 
 
 
回到控制器,进行权限验证
- [BasePage(PermissionID = 13)]
-   
- public ActionResult Index()  
-   
- {  
-   
-    
-   
- }  
 
 
 
无论对Asp.net Form或者Aap.net MVC,都在一个按钮级的权限控制,
那对于,按钮级的权限如何进行控制昵?
 
看下面代码
 
-   
- <% if((User as account.ContextUser.MyPrincipal).PermissionList.Contains(13) {%>  
-   
- <input type="submit" name="button" id="button" value="删除" />  
-   
- <%} %>  
 
 
 
至此,如何实现自定义Identity和Principal,进行整合更多用户信息,和权限验证。己经介绍完了。
.NET 实现自定义ContextUser的Identity和Principal实现自定义用户信息,权限验证。,布布扣,bubuko.com
.NET 实现自定义ContextUser的Identity和Principal实现自定义用户信息,权限验证。
标签:blog   http   java   使用   strong   数据   
原文地址:http://www.cnblogs.com/huangzelin/p/3821415.html