How to use "User.Identity.IsAuthenticated" in the web API

User.Identity.IsAuthenticatedalways returns falseASP.NET web APIs in my project.

In my account ApiController, I have the following:

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

After logging in, User.Identity.IsAuthenticatedalways falsein ApiController but truein the MVC controller.

+4
source share
3 answers

Cannot use the HttpContext property directly in APIControiller. To get this, you must use the Request property of type System.Net.Http.HttpRequestMessage. HttpRequestMessage has a property dictionary; You will find that the MS_UserPrincipal key value contains your IPrincipal object.

+2

ApiController :

base.User.Identity.IsAuthenticated
+2
var isAusorized = (Request.Properties["MS_HttpContext"] as HttpContextWrapper).User.Identity.IsAuthenticated;
0
source

Source: https://habr.com/ru/post/1531147/


All Articles