重写 AuthorizeAttribute 的 OnAuthorization 方法:

using System.Web.Mvc;

namespace Demo.Web.Common
{
    public class AuthorizeUserAttribute : AuthorizeAttribute
    {
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            base.OnAuthorization(filterContext);
            if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
            {
                return;
            }
            if (!Demo.ExternalService.UserService.IsUserInRole(filterContext.HttpContext.User.Identity.Name))
            {
                filterContext.Result = new RedirectResult("http://www.test.com");//身份验证不通过,则跳转至此网站。
            }
        }
    }
}

 


Controller 控制器配置:

using System.Web.Mvc;

namespace Demo.Ad.Web.Controllers
{
    [AuthorizeUser]//可以在 Controller 上直接配置,作用于此 Controller 下所有 Action
    public class IndexController : Controller
    {
        //[AuthorizeUser]
        public ActionResult Index()
        {
            return View();
        }
    }
}

 

相关文章:

  • 2022-12-23
  • 2022-02-19
  • 2022-12-23
  • 2021-11-13
  • 2021-06-11
  • 2021-07-11
  • 2022-01-14
  • 2022-12-23
猜你喜欢
  • 2021-10-16
  • 2022-12-23
  • 2022-12-23
  • 2022-02-26
  • 2022-12-23
  • 2022-02-12
  • 2021-06-04
相关资源
相似解决方案