在MVC中需要有关Forms身份验证Logout方法的帮助。 [英] Need help on Forms authentication Logout method in MVC.

查看:72
本文介绍了在MVC中需要有关Forms身份验证Logout方法的帮助。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我对MVC很陌生。从页面上的简单登录和注销链接开始。

当我点击注销链接时,表单实际上没有注销。

这是我的代码,



Hi,
I am pretty new to MVC. Started with simple login and logout links on a page.
When I click on Log out link, the form is not logging out actually.
Here's my code,

[HttpPost]
      public ActionResult LogOut()
      {
          if (Request.IsAuthenticated)
          {
              FormsAuthentication.SignOut();
          }
          return View();
      }





我甚至尝试过其他一些东西,比如Response.Cookies.Clear();但是没用。



如果登录成功,我想在菜单中显示一些链接以导航到其他页面。

所以我拿了_loginpartial视图并保存在Layout页面中。

_loginpartial视图是





I even tried some other stuff like "Response.Cookies.Clear();" but no use.

If a log in is successful, I want to display some links in the menu to navigate to other pages.
So I took a _loginpartial view and kept in a Layout page.
The _loginpartial view is

@if (Request.IsAuthenticated) {
    <text>
        <div class="float-right">
        Hello, @Html.Encode(User.Identity.Name) 
         @using (Html.BeginForm("LogOut", "User", FormMethod.Post, new { id = "logoutForm" })) {
            <a href="java<!-- no -->script:document.getElementById('logoutForm').submit()">Log out</a>
        }
            <br /><br />
                    
                            @Html.ActionLink("Publishers", "Index", "Publisher")
                            @Html.ActionLink("Customers", "Index", "Customer") 
                            @Html.ActionLink("Authors", "Index", "Author")
                     
                </div>
    </text>
} else {
    
        @Html.ActionLink("Register", "Register", "User", routeValues: null, htmlAttributes: new { id = "registerLink" })
        @Html.ActionLink("Log in", "Login", "User", routeValues: null, htmlAttributes: new { id = "loginLink" })
    
}





我甚至试图在Log out视图中将布局设为null。

我不知道我们是否可以检查Request.IsAuthenticated属性viewstart页面,但

如果用户未经过身份验证,我还尝试在viewstart中将布局设为null。



帮我解决这个问题。 。



提前致谢。



I even tried to give a layout as null in Log out view.
I don't know whether we could check the Request.IsAuthenticated property in viewstart page, but
I also tried to give a layout as null in viewstart if user is not authenticated.

Help me on this..

Thanks in advance.

推荐答案

嗨Sanjay,



是后退按钮问题还是注销问题?注销后,您是否能够按预期获得响应?我遇到类似的问题,然后一旦注销,在_logonpartial页面我检查了会话值,如果会话为空,那么我重定向到登录页面。

您也可以尝试相同的您的样本申请



注销后尝试点击任何链接,看看是否可以访问该页面。

如果你无法访问它按预期工作。



希望这对你有所帮助。



问候,

RK
Hi Sanjay,

Is it a back button issue or logout issue? Once you logout can you able to get the response as expected? I faced similar kind of issue, then once logged out, in _logonpartial page I checked for the session value, if the session is empty then i redirected to the login page.
You also can try the same in your sample application

Try to click any of the link once logged out and see whether you can access the page or not.
If you cannot access then its working as expected.

Hope this helps you a bit.

Regards,
RK


您可以创建一个动作过滤器,您需要随动作一起放置。



在此动作过滤器(
You can create a Action Filter that you need to place with the action.

And In this Action Filter (
OnActionExecuting

OnActionExecuted

)中,您可以编写身份验证代码。

) you can write your authentication code.


这篇关于在MVC中需要有关Forms身份验证Logout方法的帮助。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆