Redirecting partial browsing to the login page when the session expires

Is there an easy way to redirect the entire page (not just a partial view) to the login page after the session expires?

I tried the following solutions, but can't get it to work:

My problem is that the partial view is redirected to the login page, and not to the whole page (the same problem as in the links).

controller

        [HttpPost]
        public PartialViewResult LogPartialView(string a, int? b, string c, string d, int? e, string f)
        {
            //If the user is "Admin" -> display Logs for all customers.
            if (Roles.IsUserInRole(WebSecurity.CurrentUserName, "Admin"))
            {
                if (Session["myID"] == null)
                {
                    ExpireSession();
                }
            //Some code

        return PartialView("LogPartialLayout", model);
        }

I wanted to return Redirect ("~ /") if myID is null, but it does not work, since it expects a partial view.

: "System.Web.Mvc.RedirectResult" "System.Web.Mvc.PartialViewResult"

public void ExpireSession()
    {
        Session.Abandon();
        WebSecurity.Logout();
        Response.Redirect("~/");

    }

View Image

+4
2

@EmilChirambattu.

[HttpPost]
public ActionResult LogPartialView(string a, int? b, string c, string d, int? e, string f)
{
    // You should check the session before anything else.
    if (Session["myID"] == null)
    {
        return ExpireSession();
    }

    //If the user is "Admin" -> display Logs for all customers.
    if (Roles.IsUserInRole(WebSecurity.CurrentUserName, "Admin"))
    {
        //Some code
    }

    return PartialView("LogPartialLayout", model);
}

public void ExpireSession()
{
    Session.Abandon();
    WebSecurity.Logout();
    Response.Redirect("RedirectToLogin");
}

public ActionResult RedirectToLogin()
{
    return PartialView("_RedirectToLogin");
}

_RedirectToLogin View

<script>
    window.location = '@Url.Action("Index", "")';
</script>

URL- ( , ).

+2

-

<authentication mode="Forms">
  <forms loginUrl="~/Account/RedirectToLogin" timeout="2880" />
</authentication>

public ActionResult RedirectToLogin()
{
    return PartialView("_RedirectToLogin");
}

_RedirectToLogin View

<script>
    window.location = '@Url.Action("Login", "Account")';
</script>

- , URL-

+2

Source: https://habr.com/ru/post/1648468/


All Articles