ASP.net MVC 5:重定向到登录页面

时间:2017-10-09 02:12:24

标签: c# asp.net asp.net-mvc asp.net-mvc-4

我有一个会话过期属性

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var context = filterContext.HttpContext;
        if (context.Session != null)
        {
            if (context.Session.IsNewSession)
            {
                string sessionCookie = context.Request.Headers["Cookie"];

                if ((sessionCookie == null) && (sessionCookie.IndexOf("ASP.NET_SessionId") < 0))
                {
                    FormsAuthentication.SignOut();
                    filterContext.Result = new RedirectResult("~/Login/Index");
                }
            }
        }

        base.OnActionExecuting(filterContext);
    }

和我的控制器

public ActionResult Index(Login login)
    {
        //WebSecurity.Login
        User user = new User();
        string encodePassword = Crytography.EncodePasswordToBase64(login.Password);
        var consumer = user.Get(u => u.f_username == login.Username && u.f_password == encodePassword).SingleOrDefault();
        if (consumer != null)
        {
            FormsAuthentication.SetAuthCookie(login.Username, false);
            var expiration = 3;
            var authTicket = new FormsAuthenticationTicket(1, login.Username, DateTime.Now, DateTime.Now.AddSeconds(expiration), false, "Admin");
            var encryptedTicket = FormsAuthentication.Encrypt(authTicket);
            var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
            HttpContext.Response.Cookies.Add(authCookie);
            return RedirectToAction("Index", "Main");
        }
        return View();

我的问题是当重定向主页的导航时仍然存在。

Navigation Stays and the url is the same

enter image description here

0 个答案:

没有答案