示例#1
0
        public void OnAuthorization(AuthorizationContext filterContext)
        {
            Func <string, bool> predicate = null;

            if (filterContext.HttpContext.User == null || filterContext.HttpContext.User.Identity.IsAuthenticated == false)
            {
                filterContext.Result = new HttpUnauthorizedResult();
                return;
            }
            if (filterContext.HttpContext.User is FanxiPrincipal)
            {
                //filterContext.HttpContext.Request.ValidateInput();
                FanxiPrincipal currentUser = filterContext.HttpContext.User as FanxiPrincipal;
                UserIdentity   Id          = currentUser.Identity as UserIdentity;
                if (_Users != null && _Users.Length > 0)
                {
                    if (!_Users.Contains(Id.Name))
                    {
                        filterContext.Result = new HttpUnauthorizedResult(); return;
                    }
                }
                if (_Roles != null && _Roles.Length > 0)
                {
                    if (predicate == null)
                    {
                        predicate = delegate(string r)
                        {
                            return(this._Roles.Contains <string>(r));
                        };
                    }
                    IEnumerable <string> TempRoles = (from r in Id.Roles where _Roles.Contains(r) select r);
                    if (TempRoles == null || TempRoles.Count() == 0)
                    {
                        filterContext.Result = new HttpUnauthorizedResult(); return;
                    }
                }
                if (_Permissions != null && _Permissions.Length > 0)
                {
                    string[] HasPermission = (from per in Id.Permissions select per.Name).ToArray();
                    string[] TempPer       = (from per in _Permissions where (!HasPermission.Contains(per)) select per).ToArray();
                    if (TempPer != null && TempPer.Length > 0)
                    {
                        filterContext.Result = new HttpUnauthorizedResult(); return;
                    }
                }
                if (_Object_Operations != null && _Object_Operations.Length > 0)
                {
                    string[] HasPermission = (from per in Id.Permissions select per.RbacObject + "|" + per.RbacOperation).ToArray();
                    string[] TempPer       = (from per in _Object_Operations where (!HasPermission.Contains(per)) select per).ToArray();
                    if (TempPer != null && TempPer.Length > 0)
                    {
                        filterContext.Result = new HttpUnauthorizedResult(); return;
                    }
                }
            }
            else
            {
                filterContext.Result = new HttpUnauthorizedResult();
            }
        }
 void OnAuthenticate(object sender, EventArgs e)
 {
     HttpApplication app = (HttpApplication)sender;
     if (app.Context.User != null && app.Context.User.Identity.IsAuthenticated)
     {
         HttpCookie authenCookie = app.Context.Request.Cookies.Get(FormsAuthentication.FormsCookieName);
         if (authenCookie == null) return ;
         FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authenCookie.Value);
         UserIdentity Id = new UserIdentity(ticket);
         FanxiPrincipal _principal = new FanxiPrincipal(Id);
         app.Context.User = _principal;
     }
 }
示例#3
0
        void OnAuthenticate(object sender, EventArgs e)
        {
            HttpApplication app = (HttpApplication)sender;

            if (app.Context.User != null && app.Context.User.Identity.IsAuthenticated)
            {
                HttpCookie authenCookie = app.Context.Request.Cookies.Get(FormsAuthentication.FormsCookieName);
                if (authenCookie == null)
                {
                    return;
                }
                FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authenCookie.Value);
                UserIdentity   Id         = new UserIdentity(ticket);
                FanxiPrincipal _principal = new FanxiPrincipal(Id);
                app.Context.User = _principal;
            }
        }