4

I am trying to implement Logout Functionality in ASP.NET MVC.

I use Forms Authentication for my project.

This is my Logout code:

FormsAuthentication.SignOut();
Response.Cookies.Clear();
FormsAuthenticationTicket ticket = 
    new FormsAuthenticationTicket(
        1,
        FormsAuthentication.FormsCookieName,
        DateTime.Today.AddYears(-1),
        DateTime.Today.AddYears(-2),
        true,
        string.Empty);

Response.Cookies[FormsAuthentication.FormsCookieName].Value = 
            FormsAuthentication.Encrypt(ticket); 
Response.Cookies[FormsAuthentication.FormsCookieName].Expires = 
            DateTime.Today.AddYears(-2);

return Redirect("LogOn");

This code redirects the user to the Login Screen. However, if I call an action method by specifying the name in address bar (or select the previous link from address bar dropdown), I am still able to reach the secure pages without logging in.

Could someone help me solve the issue?

9
  • Why are u not using FormsAuthentication.SignOut() ? Commented Oct 8, 2010 at 9:55
  • @Aliostad : Now changed the source code , previously the method call was wrapped. Commented Oct 8, 2010 at 10:04
  • @vijaysylvester - Could you please give us some details about how you actually make your secure pages secure? Commented Oct 8, 2010 at 10:20
  • I have configured them in Web.Config to deny access for users not authenticated. Commented Oct 8, 2010 at 10:28
  • @vijaysylver - there's your problem. This is not web forms, this is MVC. Venemo's answer is spot on - you need to decorate the action methods with authorize. I bet even if you didnt log in, you could get to any secure page. Commented Oct 8, 2010 at 10:30

4 Answers 4

6

That's strange... I make one single call to: FormsAuthentication.SignOut(); and it works...

public ActionResult Logout() {
  FormsAuthentication.SignOut();
  return Redirect("~/");
}
Sign up to request clarification or add additional context in comments.

1 Comment

I used reflector and saw what the FormsAuth.SignOut() was doing. It does the same thing i am trying to accomplish. that is setting the cookie's expires property to the previous date. But it carefully adds the cookie to the response , but i dint do that , that is the issue here. Thanks for the help!
1

To correctly answer your question, I'd have to know how do you secure your "secure" pages.
I suspect that you're doing something wrong there.

A simple call to FormsAuthentication.SignOut() should be enough, as it clears the authentication cookie, thus making the other method calls you make there redundant.

With ASP.NET MVC, you have to use the AuthorizeAttribute on an action method to disallow non-authenticated visitors to use it. (Meaning: the old way you did it with Web Forms by specifying location tags in Web.config no longer works with MVC.)

For example, here is a small code snippet from my ForumController class:

public class ForumController : Controller
{
    ...

    [Authorize]
    public ActionResult CreateReply(int topicId)
    {
        ...
    }

    ...
}

8 Comments

To be clear , Even if you clear the cookie at server side using SignOut(), The Client passes the Forms Auth Cookie at the next request. To prevent this i am adding a cookie with same name , but with an expired time.
@vijaysylvester - Well then, how do you explain that it works for me and @Palantir too?
@vijaysylvester - you are correct that the cookie will still be sent (until it is expired), but it wont matter, as this cookie will be seen as 'stale' to ASP.NET. There are other steps to bolster this, such as absolute expiration and SSL - but the 'hack' of adding a cookie with the same name is not one of them. I dont think FormsAuth is the problem here.
@Venemo: Cheers mate! You both were successful in the 1st line that is FormsAuthentication.SignOut() , Since i was not , i tried other options as well. As simple as that.
Im confused, are you saying you didnt have FormsAuthentication.SignOut() in your original code? Then why is it in the question?
|
1

The following question is related the it's solution works for me

FormsAuthentication.SignOut() does not log the user out

Comments

0

This method works, if you do not disable[comment] the following tags in the web.config file to test your web application easily.

public ActionResult SignOut()
{
    FormsAuthentication.SignOut();
    return RedirectToAction("Index", "Home");
}

web.config

<authentication mode="Forms">
  <forms name="Your Project Name" defaultUrl="/" loginUrl="/Users/Login" timeout="43200" />
</authentication>

<location path="Administrator">
  <system.web>
    <authorization>
      <allow roles="Administrator" />
      <deny users="*" />
    </authorization>
  </system.web>
</location>

<location path="UserPanel">
  <system.web>
    <authorization>
      <deny users="?" />
    </authorization>
  </system.web>
</location>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.