ElDog
ElDog

Reputation: 1377

MVC: proper authentication

In my controller I have a login method. There I check credentials and if everything is ok call

FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);

It works but if I immediately check

this.User.Identity.IsAuthenticated

inside a controller I see that it is still false. I need to refresh a page to see that user is indeed authenticated. Is there a possibility to make user authenticated immediately, during the same request? Other partial views which are rendered after depend on it.

In web forms development I would call something like

RedirectFromLoginPage

but here it is inside partial view rendered from a layout file, I can't redirect from there.

Upvotes: 0

Views: 162

Answers (1)

Tommy
Tommy

Reputation: 39807

The call FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe); sets an encrypted authentication cookie. Because this is a cookie, the call for IsAuthenticated is not immediate as the user has to make another request to the server in order for the cookie to be sent to/from the client.

Upvotes: 1

Related Questions