Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
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,

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


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

SQL
@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" })
    
}


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.
Posted

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
 
Share this answer
 
Comments
sanjay8589 8-Nov-13 11:48am    
Thanks rk_prabakar, I am able to redirect using a Session variable.
In _loginpartial view, I've checked the session value is null or not.

Thanks once again.
♥…ЯҠ…♥ 8-Nov-13 22:56pm    
If it helps you resolve the problem, mark it as answer..... ;-)
sanjay8589 9-Nov-13 7:39am    
yeah I clicked on accept solution.
Does hitting accept solutions increases the repliers reputation at CP?
♥…ЯҠ…♥ 9-Nov-13 7:46am    
At the same time it would help others to identify the solution, if it solves your problem then definitely will be a solution to anyone right?
You can create a Action Filter that you need to place with the action.

And In this Action Filter (
OnActionExecuting
and
OnActionExecuted
) you can write your authentication code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900