C# 我可以从 Web API 访问 IIdentity

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9768872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 08:46:38  来源:igfitidea点击:

Can I access IIdentity from Web API

c#asp.net-web-api

提问by Magpie

I'm adding some Web API services to an existing MVC application. I have a model binder for my MVC controllers to get the user object stored in a CustomIdentity. I'm trying to reproduce this for my Web API actions.

我正在向现有的 MVC 应用程序添加一些 Web API 服务。我有一个用于我的 MVC 控制器的模型绑定器来获取存储在 CustomIdentity 中的用户对象。我正在尝试为我的 Web API 操作重现此内容。

In the MVC Controller or its binders I can use

在 MVC 控制器或其绑定器中我可以使用

controllerContext.HttpContext.User.Identity

The ApiController doesn't have the HttpContext object. Is there anyway to access the IIdentity object from the Web API?

ApiController 没有 HttpContext 对象。有没有办法从 Web API 访问 IIdentity 对象?

采纳答案by mhu

They removed GetUserPrincipal in ASP.NET MVC 4 RC. However it seems the ApiController property Userhas replaced this: http://msdn.microsoft.com/en-us/library/system.web.http.apicontroller.user

他们在 ASP.NET MVC 4 RC 中删除了 GetUserPrincipal。然而,似乎 ApiController 属性User已经取代了这个:http: //msdn.microsoft.com/en-us/library/system.web.http.apicontroller.user

回答by James Webster

Yes you can. ApiControllerhas a Requestproperty of type System.Net.Http.HttpRequestMessage; this holds details about the current request naturally (it also has a setter for unit testing purposes). HttpRequestMessagehas a Propertiesdictionary; you will find the value of the key MS_UserPrincipalholds your IPrincipalobject.

是的你可以。ApiController有一个Request类型的属性System.Net.Http.HttpRequestMessage;这自然包含有关当前请求的详细信息(它还具有用于单元测试目的的设置器)。HttpRequestMessageProperties字典;你会发现键的值MS_UserPrincipal保存了你的IPrincipal对象。

In researching this answer, I came across the System.Web.Http.HttpRequestMessageExtensionswhich has a GetUserPrincipal(this HttpRequestMessage request)extension method which accesses this dictionary value; I hadn't seen this extension method before and was accessing Request.Properties["MS_UserPrincipal"]directly, but this might be a better method (less dependent on the ASP.NET Web Api team keeping the name of the key the same...)

在研究这个答案时,我遇到了System.Web.Http.HttpRequestMessageExtensions一个GetUserPrincipal(this HttpRequestMessage request)访问这个字典值的扩展方法;我之前没有见过这个扩展方法,Request.Properties["MS_UserPrincipal"]直接访问,但这可能是一个更好的方法(较少依赖 ASP.NET Web Api 团队保持密钥名称相同......)

回答by abatishchev

You can try to use this extension methodfrom System.ServiceModel.dll:

您可以尝试使用System.ServiceModel.dll 中的此扩展方法

public static IPrincipal GetUserPrincipal(this HttpRequestMessage request);

as:

作为:

IPrincipal principal = request.GetUserPrincipal();
IIdentity identity = principal.Identity;

回答by LuSK3

I had the same problem and I found this way. In your ApiController you can use
base.ControllerContext.RequestContext.Principal.Identity.;

我有同样的问题,我找到了这种方式。在您的 ApiController 中,您可以使用
base.ControllerContext.RequestContext.Principal.Identity.;