如何使用" User.Identity.IsAuthenticated"在网页API [英] How to use "User.Identity.IsAuthenticated" in Web API

查看:163
本文介绍了如何使用" User.Identity.IsAuthenticated"在网页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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆