简体   繁体   中英

Hook into login (MVC4)

Is there any way to hook into (I want to perform a check for certain conditions directly after login) the login process in ASP.NET MVC4. I am using the default MemberShip classes provided by Visual Studio.

Yes, you can create your on AuthorizeAttribute and implement your login on OnAuthorization method.

Here's a sample:

  public class AuthenticateAndAuthorizeAcsMvcAttribute : System.Web.Mvc.AuthorizeAttribute
{
    public override void OnAuthorization(System.Web.Mvc.AuthorizationContext filterContext)
    {
        var principal = filterContext.HttpContext.User;
        if (principal == null || !principal.Identity.IsAuthenticated)
        {
            filterContext.Result = new ViewResult()
            {
                ViewName = "AcsLogin",
                ViewData = filterContext.Controller.ViewData,
                TempData = filterContext.Controller.TempData
            };
            return;

        }
        base.OnAuthorization(filterContext);
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM