1

As I am working on Asp.Net MVC 3 application, I have used FormAuthentication in my application.

The problem is, after logged in into system, when I close browser (without logout) and again open the page (let's say /Admin/ProductList/) in browser, the page is still being invoked and I got focus in my controller too. [Which is really bad! :( ]

What I want is, when I close browser and come back again on any page, it should goes to logged in page.

Please review the given code for your understanding.

public void SignIn(string userName, bool isCookiePersistent)
        {

            FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, userName, DateTime.Now, DateTime.Now.AddDays(14),
                createPersistentCookie, string.Empty);

            HttpCookie authCookie = FormsAuthentication.GetAuthCookie(userName, isCookiePersistent);
            if (authTicket.IsPersistent)
            {
                authCookie.Expires = authTicket.Expiration;
            }

            authCookie.Value = FormsAuthentication.Encrypt(authTicket);
            HttpContext.Current.Response.Cookies.Add(authCookie);
        }

public void SignOut()
        {
            FormsAuthentication.SignOut();
        }

Web.Config code:

<authentication mode="Forms">
      <forms loginUrl="~/Admin/Login" timeout="2880" />
    </authentication>

My page is getting in **Redirection Loop**: This is the main issue.

Am I missing any other settings or global.asax event handling?

Please help me by giving me any resolution.

Thanks in advance.

1 Answer 1

3

Here:

authCookie.Expires = authTicket.Expiration;

That's what makes the authentication cookie persistent and the browser stores it on the client computer so that when you restart the browser the cookie is still there. If you don't want persistent cookies you could try this:

public void SignIn(string userName)
{
    var authTicket = new FormsAuthenticationTicket(
        1, userName, DateTime.Now, DateTime.Now.AddDays(14), false, string.Empty
    );
    var authCookie = FormsAuthentication.GetAuthCookie(userName, false);
    authCookie.Value = FormsAuthentication.Encrypt(authTicket);
    HttpContext.Current.Response.Cookies.Add(authCookie);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for reply! But this is not working, actually when I close browser and come back again with any url, the page shows as a BLANK! NOTHING IS HAPPENING.... it opens as a blank white page...
@nunu, is the action you are trying to navigate to decorated with the [Authorize] attribute? That's what will cause the redirect to the login screen if the user is not authenticated.
No, I have NOT used [Authorize] attribute. Actually when I come back to any page, it is opening with BLANK WHITE PAGE, no matter for what page I made request...
@nunu, you should decorate all controller actions that you want to be authenticated for with the [Authorize] attribute. In order to avoid decorating all of them you could only decorate the controller. As far as the blank page is concerned you might have some cookie left on your browser. Try clearing all cookies before trying again.

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.