首页 新闻 会员 周边

MVC4 用户退出页面需刷新后才会返回登陆页面

0
悬赏园豆:15 [已解决问题] 解决于 2015-08-16 16:19

1.Web.config配置未登录用户的返回路径

1 <system.web>
2     <compilation debug="true" targetFramework="4.5" />
3     <httpRuntime targetFramework="4.5" />
4     <authentication mode="Forms">
5       <forms loginUrl="/Account/Login" name="UserName"></forms>
6     </authentication>
7   </system.web>

2.新建了一个UserAuthorizeAttribute类继承AuthorizeAttribute

 1  public class UserAuthorizeAttribute : AuthorizeAttribute
 2     {
 3         protected override bool AuthorizeCore(HttpContextBase httpContext)
 4         {
 5             string cookieName = FormsAuthentication.FormsCookieName;
 6             string userName = HttpContext.Current.User.Identity.Name;
 7             if (userName == "")
 8             {
 9                 return false;
10             }
11             else
12             {
13                 return true;
14             }
15         }
16     }
View Code

3.触发注销

 $("#logOff").click(function () {
        $.post("/Account/Sin");
    });

4.AccountController

namespace Grwl.CMS.Web.Controllers
{
    [UserAuthorize]
    public class AccountController : Controller
    {
        //
        // GET: /Master/Account/

        #region 用户登录
        [AllowAnonymous]
        public ActionResult Login()
        {
            return View();
        }
        /// <summary>
        /// 用户登录,对访问数据库进行验证
        /// </summary>
        /// <param name="userInfo">数据实体</param>
        /// <returns></returns>
        [AllowAnonymous]
        public ActionResult UserLogin(UserInfo userInfo)
        {
            string userName = userInfo.userName;
            string userPwd = userInfo.userPwd;
            UserInfoService userInfoService = new UserInfoService();
            userInfo = userInfoService.GetUserInfo(userName, userPwd);
            if (userInfo != null)
            {
                FormsAuthentication.SetAuthCookie(userName,false);
                return Content("ok:登陆成功");
            }
            else
            {
                return Content("no:用户名密码错误");
            }
        }
        #endregion
        #region 注销
        [HttpPost]
        public ActionResult Sin()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Login", "Account");
        }
        #endregion
    }
}

我点击注销按钮激发了Sin,也执行了Account/Login,还会停留在当前页面,需要刷新一次才会自动跳转到登陆页面,怎么回事

戒不掉n_思念的主页 戒不掉n_思念 | 初学一级 | 园豆:135
提问于:2015-06-25 15:25
< >
分享
最佳答案
1

在Ajax里面在做一次判断,如果是登录页面,那么用js导向到登录页。

收获园豆:15
幻天芒 | 高人七级 |园豆:37175 | 2015-06-25 22:22
 $("#logOff").click(function () {
        $.post("/Account/Sin", function() {
            location.href = "/Home/Index";
        });
    });

我把JS改成这样了,退出后会跳转到登陆界面,我不明白的是,为什么我最开始写的

return RedirectToAction("Login", "Account");

它是会执行Login的,为什么不返回Login视图呢

 [AllowAnonymous]
        public ActionResult Login()
        {
            return View();
        }

 

戒不掉n_思念 | 园豆:135 (初学一级) | 2015-06-26 09:43

@戒不掉n_思念: 在视图中指定视图名称,就可以返回了。

幻天芒 | 园豆:37175 (高人七级) | 2015-06-26 11:49

@幻天芒: 

[HttpPost]
        public ActionResult Sin()
        {
            FormsAuthentication.SignOut();
            return View("Login");
        }

我改成这样也需要刷新一起才会有页面跳转

戒不掉n_思念 | 园豆:135 (初学一级) | 2015-06-26 14:19

@戒不掉n_思念: 肯定要刷新才会有跳转,ajax可是局部刷新,不过这样可以在ajax中看到返回的页面是Login。

幻天芒 | 园豆:37175 (高人七级) | 2015-06-26 21:23
其他回答(3)
0

你这个是Ajax调用啊,Sin()方法里面的return RedirectToAction("Login", "Account");没起任何作用。

webaspx | 园豆:1973 (小虾三级) | 2015-06-25 15:37
0

不要用ajax,直接页面跳转导航至/Account/Sin页面

JeffWong | 园豆:2328 (老鸟四级) | 2015-06-25 16:59
0

这个问题就是  由于你是使用ajax提交, ajax会重新启一个新http请求,注意是新的,然后服务返回回来内容是放到你新启的这个http请求中,和你当前浏览器没有关系, 然后return RedirectToAction("Login", "Account");是通知浏览器需要让这个地址跳转,而你是ajax提交 所以这个是通知你新启的http中了,不是你真实的浏览器地址

你可以这样测试:

$("#logOff").click(function () {

    $.post("/Account/Sin", function(data) {

  alert(data);  //data是服务返回的内容,  这里你会看到你login页面的html

  });

});

jio92 | 园豆:1064 (小虾三级) | 2015-06-26 14:30

这种要怎么解决呢,我还有一个问题也是这样,点击一个连接弹出一个窗口,等登陆信息过期后会直接在弹窗显示登陆页面

支持(0) 反对(0) 戒不掉n_思念 | 园豆:135 (初学一级) | 2015-06-26 14:34

@戒不掉n_思念: 

你可以在Sin里面return 状态 而不是RedirectToAction("Login", "Account") 然后在data来判定

如:return Json(new{state=true});

$("#logOff").click(function () {

    $.post("/Account/Sin", function(data) {

    if(data.state)

  {//跳转}else{//提示}

  });

});

支持(0) 反对(0) jio92 | 园豆:1064 (小虾三级) | 2015-06-26 17:00
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册