user3405759
user3405759

Reputation: 63

How to use "User.Identity.IsAuthenticated" in Web API

User.Identity.IsAuthenticated always returns false in my ASP.NET Web API project.

In account ApiController I have following:

ClaimsIdentity identity = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie); 
AuthenticationManager.SignIn(new AuthenticationProperties() { 
    IsPersistent = isPersistent 
}, identity);

After signing in, User.Identity.IsAuthenticated is always false in ApiController but true in MVC Controller.

Upvotes: 5

Views: 5453

Answers (3)

OzBob
OzBob

Reputation: 4520

The ApiController has a User property and is available from:

base.User.Identity.IsAuthenticated

Upvotes: 7

Grisha
Grisha

Reputation: 99

var isAusorized = (Request.Properties["MS_HttpContext"] as HttpContextWrapper).User.Identity.IsAuthenticated;

Upvotes: 0

Hamid Narikkoden
Hamid Narikkoden

Reputation: 861

It's unable to use HttpContext property directly in APIControiller. To get this , you have to make use of Request property of type System.Net.Http.HttpRequestMessage. HttpRequestMessage has a Properties dictionary; you will find the value of the key MS_UserPrincipal holds your IPrincipal object.

Upvotes: 2

Related Questions