成功登录后重定向用户

时间:2014-08-11 03:55:13

标签: asp.net-mvc

我知道会话结束后。我可以将用户重定向到登录页面。

有没有办法在成功登录到会话到期之前正在查看的页面后重定向用户?

例如。用户' U'查看页面' BeforeSessionExpired'会话已过期重定向到“登录页面”

成功登录重定向到' BeforeSessionExpired'。

我将ASP.Net MVCForm Authentication一起使用。

2 个答案:

答案 0 :(得分:1)

检查web.config文件中是否存在以下行

<appSettings>
   ...
   <add key="PreserveLoginUrl" value="true" />
</appSettings>

...

<authentication mode="Forms">
      <forms loginUrl="~/Account/LogOn" timeout="43200" /> <!--43,200 in minutes - 30 days-->
</authentication>

或者将以下行添加到我的web.config文件的AppSettings部分

<add key="autoFormsAuthentication" value="false" />
<add key="enableSimpleMembership" value="false"/>

或在您的登录视图中,您是否将网址传递给Post方法?

@using (Html.BeginForm("LogOn", "Account", new { returnUrl= Request.QueryString["ReturnUrl"] }))
 { 
   <input type="submit" value="Login" />
 }

答案 1 :(得分:0)

假设您使用Login控制器并使用Index方法呈现登录页面。另外,假设您有一个带有操作Account的控制器Index,它会呈现您假设的BeforeSessionExpired页面。

Account/Index行动

if not ((System.Web.HttpContext.Current.User != null) and System.Web.HttpContext.Current.User.Identity.IsAuthenticated)  then
   Session("LastURL") = "Account"
   RedirectToAction("Login","Index")
end if

Login/Index行动

成功登录后

if not string.IsNullorWhiteSpace(Session("LastURL"))then

    RedirectToAction(Session("LastURL"),"Index")
end if

当然这只是示例代码,您可以根据需要修改代码。