ASP.NET MVC user friendly 401 error

后端 未结 4 1712
太阳男子
太阳男子 2020-12-05 07:56

I have implemented errors handling in ASP.NET MVC site in a way like suggests this post.

With 404 errors all works fine. But how correctly show user friendly screen

4条回答
  •  情深已故
    2020-12-05 08:43

    In one of my project, I use the code from uvita.

    I have ASP.NET MVC2 and I use Active Directory authentication without login page. I have a NoAuth.aspx page that use site master page, integrate the web application layout.

    This is the web.config.

    
        
        
           
              
              
          
        
    
    

    The new class CustomAutorizeAttribute

    using System.Web.Mvc;
    public class CustomAuthorizeAttribute : AuthorizeAttribute
    {
        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
            if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
            {
                base.HandleUnauthorizedRequest(filterContext);
            }
            else
            {
               filterContext.Result = new ViewResult { ViewName = "NoAuth"};
            }
        }
    }
    

    and the controller

    [CustomAuthorize(Roles = "ADRole")]
    public class HomeController : Controller
    {
        public HomeController()
        {
        }
    }
    

提交回复
热议问题