MVC 3 dynamic authorization of multiple roles and users

后端 未结 3 1916
春和景丽
春和景丽 2020-12-13 00:49

I recently starded developing for MVC 3 but have experience in both C# and ASP.NET since earlier. So i\'ll start with what i\'m trying to accomplish. I\'ve developed a small

3条回答
  •  天涯浪人
    2020-12-13 01:40

    I got it working as I wanted when I overrode the AuthorizeCore method and authorizes the way I want to.

        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            if (httpContext == null)
            {
                throw new ArgumentNullException("httpContext");
            }
    
            IPrincipal user = httpContext.User;
            if (!user.Identity.IsAuthenticated)
            {
                return false;
            }
    
            if ((_usersSplit.Length > 0 && !_usersSplit.Contains(user.Identity.Name, StringComparer.OrdinalIgnoreCase)) && (_rolesSplit.Length > 0 && !_rolesSplit.Any(user.IsInRole)))
            {
                return false;
            }
    
            return true;
        }
    

提交回复
热议问题