Barka
Barka

Reputation: 8932

confused regarding forms authentication in ASP.NET MVC

Here is my config section:

<authentication mode="Forms">
    <forms loginUrl="~/Account/Login" timeout="43200" cookieless="UseCookies" slidingExpiration="true" />
</authentication>

Now in the controller I have:

FormsService.SignIn(userName, true);
var temp = User.Identity.IsAuthenticated;

The temp value sometimes is set to false and sometimes to true. When the user is myself (valid and in the system). Regardless of this value in my aspx page I have:

<% if (Context.User.Identity.IsAuthenticated)
            { %>

this always resolves to true. So what am I doing wrong in my controller? And how do I check to see if the user is authenticated in the controller?

Many thanks!

Upvotes: 2

Views: 344

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039398

You must redirect after authenticating and setting the authentication cookie. It is on the subsequent request that the user will be truly authenticated.

public ActionResult LogOn()
{
    FormsService.SignIn(userName, true);
    return RedirectToAction("authenticated");
}

Upvotes: 1

Related Questions