コード例 #1
0
        protected override bool IsAuthorized(HttpActionContext actionContext)
        {
            if (actionContext == null)
            {
                throw new ArgumentNullException("actionContext");
            }

            IPrincipal user = actionContext.ControllerContext.RequestContext.Principal;

            if (user == null || user.Identity == null || !user.Identity.IsAuthenticated)
            {
                return(false);
            }
            if (Users.Length > 0 && !(Users.IndexOf(user.Identity.Name, StringComparison.OrdinalIgnoreCase) >= 0))
            {
                return(false);
            }
            if (Roles.Length > 0)
            {
                IDependencyScope  Scope       = actionContext.Request.GetOwinContext().Get <IDependencyScope>();
                ClientUserManager UserManager = Scope.GetService(typeof(ClientUserManager)) as ClientUserManager;
                var client    = UserManager.FindByEmailAsync(user.Identity.Name).Result;
                var GroupList = UserManager.GetRolesAsync(client.Id).Result;
                var listRoles = Roles.Split(',');
                if (listRoles.Where(x => GroupList.Any(y => y.Equals(x))).Count() == 0)
                {
                    return(false);
                }
            }
            return(true);
        }