.Net MVC Partial View loads login page when session ends

I am building a web application using .net MVC 4.

I have an ajax form for editing data.

enter image description here

If the user is idle for 15 minutes, it expires with the user session. When this happens, if the user clicks the "Edit" button, he will load the login page for partial content, so the current session will now expire.

enter image description here

Change link - cshtml code

@Ajax.ActionLink("Edit", MVC.Admin.Material.ActionNames.TagEditorPanel, MVC.Admin.Material.Name, new { isView = "false", id = Model.ID.ToString() }, new AjaxOptions { HttpMethod = "GET", UpdateTargetId = "materialTagBox", InsertionMode = InsertionMode.Replace }, new { @class = "editlinks" })

Controller / Action Code

[Authorize]
public virtual ActionResult TagEditorPanel(bool isView, int id)
{
   //do something
   return PartialView(MVC.Admin.Material.Views._tag, response);
}

Web.config

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

I understand why this is happening. I do not know how to solve this. I want to prevent this, and I want to directly redirect the user to the login page. How can I achieve this?

Thanks in gratitude .. !!!

+14
6

, , , javascript.

-

<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>
+11

() .

ASP.NET MVC ajax post?

, ajax , - .

, / , , .

, .

+1

kramwens, RedirectToLogin ( ) :

<script> 
if (window.location != '@string.Format("{0}://{1}{2}",Request.Url.Scheme, Request.Url.Authority,Url.Content("~/Account/Login"))')
            window.location = '@Url.Action("Login", "Account")';
</script>

.location, , , . , js , :)

+1

.

Simple One Action, java windows.load(), URL .

// .

<script type="text/javascript">
window.location = '@Url.Action("Login", "LogIn")';
</script>

Public ActionResult SessionExpire() { return View(); } // , :

return Redirect("~/OrderPlace/Sessionview");

0

My solution is to use some C # code whenever possible. I can get the controller and see the name, check what they should be, and if not redirect to the right one.

var controllerName = ViewContext.RouteData.GetRequiredString("controller");
var actionName = ViewContext.RouteData.GetRequiredString("action");

Then I use the following to navigate to the desired URL:

if (controllerName != "members" && actionName != "logon")
{
    @{ Response.Redirect("~/Members/Logon");}
}
0
source

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


All Articles