コード例 #1
0
        protected override bool IsAuthorized(HttpActionContext actionContext)
        {
            if (Shell.AuthorizationService == null || AllowAnonymous)
            {
                return(true);
            }


            if (Shell.AuthorizationService is IAccessControlAuthorizationService)
            {
                IAccessControlAuthorizationService Auth = (IAccessControlAuthorizationService)Shell.AuthorizationService;

                string name   = Resource ?? (string)actionContext.RequestContext.RouteData.Values["controller"];
                string action = Action ?? (string)actionContext.RequestContext.RouteData.Values["action"];

                return(Auth.IsAuthorized(name, action));
            }

            return(true);
        }
コード例 #2
0
        public void IsAuthorized_Cases(string resource, string method, bool res)
        {
            RunScoped(sc =>
            {
                var auth = sc.GetService <IAuthenticationService>();
                var sess = sc.GetService <ISessionManager>();
                var lRes = auth.Login("admin", "12345");

                sess.AuthorizationRequest(lRes.Token);
                var zie = sc.GetService <IAuthorizationService>();

                IAccessControlAuthorizationService acc = (IAccessControlAuthorizationService)zie;
                AuthorizationRequest <AuthorizationFilterContext> req = new AuthorizationRequest <AuthorizationFilterContext>(null);
                req.Resource = resource;
                req.Action   = method;
                var resp     = acc.IsAuthorized(req);

                Assert.AreEqual(res, resp);
            });
        }