Выход пользователя в MVC 5 с использованием пользовательского атрибута ActionFilterAttribute

У меня есть собственный атрибут ActionFilterAttribute, который гарантирует, что значение в сеансе соответствует значению в базе данных. Если значения не совпадают, он перенаправляет пользователя к действию Login в AccountController.

public class CheckSessionAttribute : ActionFilterAttribute, IAuthenticationFilter
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (filterContext.ActionDescriptor.GetCustomAttributes(typeof(AllowAnonymousAttribute), false).Any())
        {
            // If the action allows Anonymous users, no need to check the session
            return;
        }

        var session = filterContext.RequestContext.HttpContext.Session;
        var userName = filterContext.RequestContext.HttpContext.User.Identity.Name;

        var userStore = new ApplicationUserStore(new IdentityDb());
        var userManager = new ApplicationUserManager(userStore);

        var user = userManager.FindByNameAsync(userName).Result;

        if (userName == null || user == null || session == null || session["ActiveSessionId"] == null || 
            session["ActiveSessionId"].ToString() != user.ActiveSessionId.ToString())
        {
            session.RemoveAll();
            session.Clear();
            session.Abandon();

            filterContext.Result = new RedirectToRouteResult(
                new RouteValueDictionary(new
                {
                    action = "Login",
                    controller = "Account"
                }
            ));         
        }

        base.OnActionExecuting(filterContext);
    }
}

[Authorize]
public class AccountController : Controller
{
    [AllowAnonymous]
    public ActionResult Login(string returnUrl)
    {
        SignOutAndKillSession();
        ViewBag.ReturnUrl = returnUrl;
        return View();
    }

    private void SignOutAndKillSession()
    {
        AuthenticationManager.SignOut();
        Session.RemoveAll();
        Session.Clear();
        Session.Abandon();
    }
}

Когда я пытаюсь снова войти в систему после перенаправления на действие «Вход», я получаю следующее исключение:

The provided anti-forgery token was meant for a different claims-based user than the current user

Я установил точку останова внутри действия входа и вижу, что User.Identity.Name по-прежнему установлен для пользователя, который выходит из системы, до И после вызова SignOutAndKillSession(). Я считаю, что именно это вызывает создание неправильного AntiForgeryToken при отображении страницы.

Может ли кто-нибудь помочь мне узнать, как очистить участника-пользователя при выходе из системы?

Спасибо


person thiag0    schedule 07.11.2014    source источник


Ответы (1)


Для всех, кто сталкивается с этой проблемой, я решил ее, удалив файлы cookie, созданные MVC 5, внутри CheckSessionAttribute. Я также изменил атрибут с ActionFilterAttribute на атрибут IAuthorizationFilter.

public class CheckSessionAttribute : FilterAttribute, IAuthorizationFilter
{
    public void OnAuthorization(AuthorizationContext filterContext)
    {
        if (filterContext.ActionDescriptor.GetCustomAttributes(typeof(AllowAnonymousAttribute), false).Any())
        {
            // If the action allows Anonymous users, no need to check the session
            return;
        }

        var session = filterContext.RequestContext.HttpContext.Session;
        var userName = filterContext.RequestContext.HttpContext.User.Identity.Name;

        var userStore = new ApplicationUserStore(new IdentityDb());
        var userManager = new ApplicationUserManager(userStore);

        var user = userManager.FindByNameAsync(userName).Result;

        if (userName == null || user == null || session == null || session["ActiveSessionId"] == null ||
            session["ActiveSessionId"].ToString() != user.ActiveSessionId.ToString())
        {
            session.RemoveAll();
            session.Clear();
            session.Abandon();

            ExpireCookie("ASP.NET_SessionId", filterContext);
            ExpireCookie("__RequestVerificationToken", filterContext);
            ExpireCookie(".AspNet.ApplicationCookie", filterContext);

            filterContext.Result = new RedirectToRouteResult(
                new RouteValueDictionary(new
                {
                    action = "Login",
                    controller = "Account"
                }
            ));
        }

        return;
    }

    private void ExpireCookie(string name, AuthorizationContext filterContext)
    {
        if (filterContext.RequestContext.HttpContext.Request.Cookies[name] != null)
        {
            filterContext.RequestContext.HttpContext.Response.Cookies[name].Value = string.Empty;
            filterContext.RequestContext.HttpContext.Response.Cookies[name].Expires = DateTime.Now.AddMonths(-20);
        }
    }
}
person thiag0    schedule 07.11.2014