【发布时间】:2016-07-12 12:36:52
【问题描述】:
我创建了一个类CheckSessionTimeOutAttribute,在这里我创建了一个方法,用于在会话超时时自动注销时返回 url。
当它在会话超时时自动注销时,它需要返回 url。当我编写正确的凭据时,它是登录控制器的返回视图。它不会是两个返回 url。
另一个问题是当会话超时并进入登录页面时,当我按下浏览器的返回时,它会进入上一页,这是不可能的。即使我注销时它也不起作用。
http://localhost:1563/?returnUrl=%2FElectricity
当我登录时它不会返回像http://localhost:1583/Electricity这样的网址
它去了http://localhost:1563/user
登录控制器:
public ActionResult Index()
{
ViewBag.Title = "Example.com | Login";
return View();
}
[HttpPost]
public ActionResult Index(FormCollection fc)
{
string username = fc["username"].ToString();
string password = fc["password"].ToString();
var query = (from u in db.tbl_user
where u.USERNAME == username && u.PASSWORD == password
select u).FirstOrDefault();
if (query != null){
Session["username"] = username;
Session["login"] = true;
return RedirectToAction("Index","User");
}
return View("Index");
}
类名CheckSessionTimeOutAttribute:
[AttributeUsage(AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public class CheckSessionTimeOutAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (HttpContext.Current.Session["login"] == null)
{
FormsAuthentication.SignOut();
filterContext.Result =
new RedirectToRouteResult(new RouteValueDictionary
{
{ "action", "Index" },
{ "controller", "Login" },
{ "returnUrl", filterContext.HttpContext.Request.RawUrl}
});
return;
}
}
}
web.config:
<sessionState mode="InProc" timeout="1" cookieless="false"></sessionState>
<authentication mode="Forms">
<forms loginUrl="~/Login" timeout="1">
</forms>
</authentication>
【问题讨论】:
-
显示您的登录控制器索引获取操作
-
@EhsanSajjad ,我添加了登录控制器的索引获取功能。
-
请不要将 FormCollection 用于您的模型,使用 TempData 也不是最好的主意。我会尽快发布答案!
标签: c# asp.net asp.net-mvc asp.net-mvc-4