[英]How to get user name when ASP.NET web api hosted as windows service
We are running a asp.net mvc application (client - this app is accessed through browser by user) which makes use of HttpClient
class to make calls to asp.net web api
application (server- I want to access the user information user. Person who invoked the client application). 我们正在运行一个asp.net mvc应用程序(客户端-该应用程序可通过用户通过浏览器访问),该应用程序利用
HttpClient
类调用asp.net web api
应用程序(服务器,我想访问用户信息用户。谁调用了客户端应用程序)。 This is a intranet application. 这是一个Intranet应用程序。 We are hosting asp.net web api as windows service.
我们托管asp.net Web API作为Windows服务。 How can I get the user name in this scenario?
在这种情况下如何获取用户名?
ApiController
class has a property public IPrincipal User { get; }
ApiController
类具有一个属性public IPrincipal User { get; }
public IPrincipal User { get; }
. public IPrincipal User { get; }
。 I looked this property and this is what i got: 我看了这个属性,这就是我得到的:
User.Identity.AuthenticationType = ""; User.Identity.AuthenticationType =“”; User.Identity.IsAuthenticated = false;
User.Identity.IsAuthenticated = false; User.Identity.Name = ""
User.Identity.Name =“”
Is it possible to get some valid value from User.Identity
with this kind of setup? 通过这种设置是否可以从
User.Identity
获得一些有效值?
It depends on which user you want, the user running the website service / app pool (EG networkservice), or an authenticated user that logged into your website. 它取决于您想要的用户,运行网站服务/应用程序池(EG网络服务)的用户或登录到您网站的经过身份验证的用户。 Assuming you have the user logging in and auth set up in IIS, this might do to pass along the creds, which you could then extract in your API.
假设您已经在IIS中登录了用户并进行了身份验证,那么这样做可以传递凭据,然后您可以将其提取到API中。
using (HttpClient httpClient = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true }))
Hard to say for sure without a whole bunch of details. 没有大量细节就很难确定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.