如何使用" User.Identity.IsAuthenticated"在网页API [英] How to use "User.Identity.IsAuthenticated" in Web API
问题描述
User.Identity.IsAuthenticated
总是返回假
在我的ASP.NET Web API项目。
User.Identity.IsAuthenticated
always returns false
in my ASP.NET Web API project.
在帐户 ApiController
我有以下几点:
ClaimsIdentity identity = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie);
AuthenticationManager.SignIn(new AuthenticationProperties() {
IsPersistent = isPersistent
}, identity);
登录后, User.Identity.IsAuthenticated
总是假
在 ApiController
但真正
在MVC控制器。
After signing in, User.Identity.IsAuthenticated
is always false
in ApiController
but true
in MVC Controller.
推荐答案
这是无法直接在APIControiller使用的HttpContext属性。为了得到这一点,你必须使用类型System.Net.Http.Htt prequestMessage的请求属性。 HTT prequestMessage有一个属性字典;你会发现关键MS_UserPrincipal的值保持你的IPrincipal对象。
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.
这篇关于如何使用" User.Identity.IsAuthenticated"在网页API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!