Я использую AuthController с событием OnActionExecuting, которое определяет, вошел ли пользователь в систему, и если нет, то я отправляю пользователя на страницу входа.
public class AuthController : Controller
{
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
// load session
var LoginSession = Session[Constants.USER_SESSION_NAME];
// load cookie
HttpCookie LoginCookie = System.Web.HttpContext.Current.Request.Cookies[Constants.USER_COOKIE];
// create cookie from session
if (LoginSession != null && LoginCookie == null)
{
var user = (UserLoginDto)LoginSession;
CreateCookieFromSession(user);
}
// create session from cookie
if (LoginSession == null)
{
if (LoginCookie != null)
{
if (!string.IsNullOrEmpty(LoginCookie.Value))
CreateSessionFromCookie(LoginCookie);
}
}
// if session does not exist send user to login page
if (Session[Constants.USER_SESSION_NAME] == null)
{
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary
{
{"controller", "Login"},
{"action", "Index"}
}
);
}
}
private void CreateSessionFromCookie(HttpCookie cookieObj)
{
UserLoginDto userDto = new UserLoginDto();
userDto.Id = Convert.ToInt32(cookieObj.Value.Split('&')[0]);
userDto = UserRepository.Get(userDto.Id);
Session.Add(Constants.USER_SESSION_NAME, userDto);
}
private HttpCookie CreateCookieFromSession(UserLoginDto user)
{
HttpCookie cookie = Request.Cookies[Constants.USER_COOKIE];
if (cookie == null)
{
cookie = new HttpCookie(Constants.USER_COOKIE);
cookie.Value = user.Id.ToString();
cookie.Values.Add("Name", Encryptor.encryptString(user.Name));
cookie.Values.Add("Type", Encryptor.encryptString(user.Type));
cookie.Values.Add("Token", user.Token);
cookie.Values.Add("ProfilePictureName", user.ProfilePictureName);
cookie.Values.Add("ProfilePicturePath", user.ProfilePicturePath);
}
cookie.Expires = DateTime.Now.AddYears(1);
Response.Cookies.Add(cookie);
return cookie;
}
}
Каждый другой контроллер, кроме входа, расширяет AuthController.
public class HomeController : AuthController
{
[HttpGet]
public ActionResult Index()
{
return View();
}
}
Повар ie всегда пуст, когда я пытаюсь загрузить метод OnActionExecuting. Кто-нибудь может определить проблему? Я также пытался создать повар ie в контроллере LoginController, но все равно Null.