【发布时间】:2015-02-25 17:14:34
【问题描述】:
我在我的 MVC 应用程序中使用 web Api,我想在我的 api 中使用以下代码获取用户身份:
User.Identity.Name
但它不起作用,它在控制器等其他地方运行良好!
是否有任何解决方案可以从 Web API 访问 User.Identity.Name?
【问题讨论】:
-
尝试使用 ApiController 属性 User 。看看这个:msdn.microsoft.com/en-us/library/…
-
在 Web API 2 中,它已更改为 ApiController.User 属性或 RequestContext.Principal ——它们是相同的。请注意,这可能为空。
标签: c# asp.net-mvc asp.net-web-api asp.net-web-api2