简体   繁体   English

使用IIS的基本身份验证获取WCF服务中的用户名

[英]Get username in WCF service using basic auth with IIS

I have a WCF service running in IIS Express on my local machine. 我在本地计算机上的IIS Express中运行了WCF服务。 I'm using HTTP Basic Authentication (without SSL at the moment). 我正在使用HTTP基本身份验证(目前没有SSL)。 I have a test client that I'm using to call my server. 我有一个测试客户端,我用它来调用我的服务器。

I need the name of the user that initiated the request (the username portion of the basic auth authentication). 我需要发起请求的用户的名称(基本身份验证的用户名部分)。 I realize that IIS is handling the authentication for me and is checking the username/password against Windows user accounts. 我意识到IIS正在为我处理身份验证,并且正在检查Windows用户帐户的用户名/密码。 That is fine for my purposes. 这对我的目的来说很好。 My issue is that once my service is called I can't find the username anywhere. 我的问题是,一旦我的服务被调用,我无法在任何地方找到用户名。 I assumed it would be in the Thread.CurrentPrincipal.Identity.Name , but that value is an empty string. 我假设它将在Thread.CurrentPrincipal.Identity.Name ,但该值是一个空字符串。 Is there a way to access this value? 有没有办法获取这个值?

Here is binding in case it is relevant: 如果相关,这是绑定:

<basicHttpBinding>
<binding name="basicauth" closeTimeout="00:01:00" openTimeout="00:01:00" 
receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" 
bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" 
maxBufferSize="99999999" maxBufferPoolSize="524288" maxReceivedMessageSize="99999999" 
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" 
useDefaultWebProxy="true">
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
  <security mode="TransportCredentialOnly">
    <transport clientCredentialType="Basic" realm="" />
  </security>          
</binding>
</basicHttpBinding>

UPDATE: Thanks everyone. 更新:谢谢大家。 Figured out my issue. 想出了我的问题。 I had improperly associated my endpoint and my binding so the endpoint was defaulting to some dynamic binding. 我不正确地关联了我的端点和绑定,因此端点默认为某种动态绑定。 Even though I configured my binding for basic auth the endpoint was not setup to use it. 即使我为基本身份验证配置了绑定,但端点未设置为使用它。

Once I fixed this issue the username was present in the 一旦我解决了这个问题,用户名就出现在

ServiceSecurityContext.Current.WindowsIdentity.Name

property like expected. 像预期的财产。

Thread.CurrentPrincipal.Identity.Name will normally retrieve the identity under which the WCF worker thread is being executed in IIS. Thread.CurrentPrincipal.Identity.Name通常会检索在IIS中执行WCF工作线程的标识。 This is not particularly useful information. 这不是特别有用的信息。 Have you inspected ServiceSecurityContext.Current.PrimaryIdentity.Name to see if it contains the authentication information from the server? 您是否检查过ServiceSecurityContext.Current.PrimaryIdentity.Name以查看它是否包含来自服务器的身份验证信息?

This may work - it works for windows auth. 这可能有效 - 它适用于Windows身份验证。 Can't remember if it works for basic ... worth a try. 不记得它是否适用于基础......值得一试。

OperationContext.Current.ServiceSecurityContext.WindowsIdentity.Name OperationContext.Current.ServiceSecurityContext.WindowsIdentity.Name

Since you are setting clientCredentialType = "Basic" , you will not get a Windows identity from WCF. 由于您正在设置clientCredentialType = "Basic" ,因此您将无法从WCF获取Windows标识。 If you use "Windows" instead of "Basic" and you configure WCF to support for impersonation as shown in this blog post , you should be able to get the identity user name. 如果您使用“Windows”而不是“Basic”并且您将WCF配置为支持模拟,如本博文中所示 ,您应该能够获取身份用户名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM