세션의 값이 데이터베이스의 값과 일치하는지 확인하는 사용자 지정 ActionFilterAttribute가 있습니다. 값이 일치하지 않으면 사용자가 AccountController의 로그인 액션으로 리디렉션됩니다. 사용자 지정 ActionFilterAttribute를 사용하여 MVC 5에서 사용자 로그 아웃
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을 볼 수 있습니다. SignOutAndKillSession() 호출 후 AND 호출 전에 이름이 로그 아웃중인 사용자로 설정됩니다. 나는 페이지가 렌더링 될 때 잘못된 AntiForgeryToken이 생성되도록하는 원인이라고 생각합니다.
사용자를 로그 아웃 할 때 사용자 교장을 삭제하는 방법을 찾는 사람을 찾을 수 있습니까? 이 문제로 실행
감사합니다 사람들을위한