ASP.NET MVC4 응용 프로그램이 있습니다. 로그인 페이지를 만들었습니다. 사용자가 시스템에 로그인하면 사용자의 정보를 세션에 등록합니다. 세션 변수를 검사하기위한 필터를 추가했습니다. 사용자가 시스템에 로그인하지 않은 경우 사용자를 내 로그인 컨트롤러로 리디렉션하고 싶습니다.ASP.NET MVC4 ActionFilters
public class SecurityAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.HttpContext.Session["User"] == null)
{
filterContext.HttpContext.Response.RedirectToRoute("Default", new
{
controller = "Login",
action = "DoLogin",
returnUrl = filterContext.HttpContext.Request.Url.AbsoluteUri
});
}
base.OnActionExecuting(filterContext);
}
}
컨트롤러 수준에서이 속성을 사용하고 있습니다.
[SecurityAttribute]
public class HomeController : Controller
{
public ActionResult Index()
{
ViewData["Name"] = ((UserEntity)Session["User"]).Name;
ViewData["Surname"] = ((UserEntity)Session["User"]).Surname;
return View();
}
}
OnActionExecuting 메서드는 동작을 실행하기 전에 실행되지만 내 홈 컨트롤러에서 동작 메서드 후에 발생하는 동작을 리디렉션합니다. 세션 변수가 null이기 때문에 인덱스 작업에 오류가 발생합니다. 이 문제를 어떻게 해결할 수 있습니까?