成功登录后显示退出按钮

时间:2016-02-21 10:19:33

标签: asp.net-mvc-3

我正在使用mvc3应用程序,在点击登录按钮后,我正在检查该用户是否已经过验证或未在jquery中使用ajax调用。如果用户通过身份验证,则返回true,在Onsuccess方法中,我将其重定向到主页。以下是代码:

function OnSuccess(response) {
    var mesg = $("#lblMess")[0];
   
    switch (response) {
        case true:
            var url = "/Home/Search";
            takeToHomePage(url);
            $(location).attr('href', url);
            
            break;
        case false:
            mesg.style.color = "red";
            mesg.innerHTML = "Email id or Password is incorrect";
            break;
        case "error":
            mesg.style.color = "red";
            mesg.innerHTML = "Error occured";
            break;
    }
}

直到这里工作正常。现在我在_layout.cshtml页面中添加了一个注销按钮,我在登录页面中没有可见性,所以如果我在切换开始之前显示此注销按钮且用户无效,则会显示注销按钮但是如果用户已通过身份验证并且尝试显示注销按钮在真实情况下,它不起作用。 需要帮忙。

1 个答案:

答案 0 :(得分:0)

  

现在我在_layout.cshtml页面中添加了一个注销按钮   在登录页面中没有可见性,所以如果我之前显示此注销按钮   开关启动,用户无效,然后显示注销按钮   如果用户已通过身份验证并尝试在true中显示注销按钮   然后它无法正常工作。

对服务器进行Ajax调用,以检查用户是否经过身份验证。然后你可以切换按钮显示和隐藏。 This is not the best method to do it. But talking about good approach would be to create a custom CustomPrincipal by inheriting ICustomPrincipal and may more changes。如果你刚刚开始使用MVC,我觉得这对你来说太过分了。

此解决方案仅用于切换注销按钮的可见性,我并不认为这是最佳做法。

 $.get("User/CheckUserAuthenticity",function(response){
     if(response == "true")
      {
        //button show;
      }
  });

您的控制器操作可以

    public ActionResult CheckUserAuthenticity()
    {
      return Json(User.Identity.IsAuthenticated,JsonRequestBehavior.AllowGet);
    }
相关问题