使用WCF和自托管的Windows身份验证

ng5*_*000 3 c# authentication wcf

我开发了一个非常简单的主机和客户端,我想用它来测试WCF客户端是否可以将登录的Windows用户的凭据传递给主机服务,而无需用户重新输入他们的凭据或设置安全性.

我的主机配置如下所示:

<configuration>
  <system.serviceModel>
    <services>
      <service name="WCFTest.CalculatorService" behaviorConfiguration="WCFTest.CalculatorBehavior">
        <host>
          <baseAddresses>
            <add baseAddress = "http://localhost:8000/WCFTest/CalculatorService/" />
            <add baseAddress = "net.tcp://localhost:9000/WCFTest/CalculatorService/" />
          </baseAddresses>
        </host>
        <endpoint address ="basicHttpEP" binding="basicHttpBinding" contract="WCFTest.ICalculatorService" bindingConfiguration="basicHttpBindingConfig"/>
        <endpoint address ="netTcpEP" binding="netTcpBinding" contract="WCFTest.ICalculatorService"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>

      </service>
    </services>
    <bindings>
      <basicHttpBinding>
        <binding name="basicHttpBindingConfig">
          <security mode="TransportCredentialOnly">
            <transport clientCredentialType="Windows" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="WCFTest.CalculatorBehavior">          
          <serviceAuthorization impersonateCallerForAllOperations="false"  principalPermissionMode="UseWindowsGroups" />
          <serviceCredentials >
            <windowsAuthentication allowAnonymousLogons="false" includeWindowsGroups="true" />
          </serviceCredentials>
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>
Run Code Online (Sandbox Code Playgroud)

我的客户端配置如下所示:

<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="WCFClient.Settings1" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_ICalculatorService" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    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="Windows" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
            </basicHttpBinding>
            <netTcpBinding>
                <binding name="NetTcpBinding_ICalculatorService" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
                    hostNameComparisonMode="StrongWildcard" listenBacklog="10"
                    maxBufferPoolSize="524288" maxBufferSize="65536" maxConnections="10"
                    maxReceivedMessageSize="65536">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00"
                        enabled="false" />
                    <security mode="Transport">
                        <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
                        <message clientCredentialType="Windows" />
                    </security>
                </binding>
            </netTcpBinding>
        </bindings>
        <client>
            <endpoint address="http://ldndwm286380:8000/WCFTest/CalculatorService/basicHttpEP"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ICalculatorService"
                contract="CalcService.ICalculatorService" name="BasicHttpBinding_ICalculatorService" />
            <endpoint address="net.tcp://ldndwm286380:9000/WCFTest/CalculatorService/netTcpEP"
                binding="netTcpBinding" bindingConfiguration="NetTcpBinding_ICalculatorService"
                contract="CalcService.ICalculatorService" name="NetTcpBinding_ICalculatorService">
            </endpoint>
        </client>
    </system.serviceModel>
    <userSettings>
        <WCFClient.Settings1>
            <setting name="Setting" serializeAs="String">
                <value>True</value>
            </setting>
        </WCFClient.Settings1>
    </userSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

我的客户代码是:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Press enter to start");
        Console.ReadLine();

        CalcService.ICalculatorService httpCalcService = new CalcService.CalculatorServiceClient("BasicHttpBinding_ICalculatorService");
        Console.WriteLine(httpCalcService.AddValues(new int[] { 1, 2, 3 }).Value);
        Console.ReadLine();

        CalcService.ICalculatorService TcpCalcService = new CalcService.CalculatorServiceClient("NetTcpBinding_ICalculatorService");
        Console.WriteLine(httpCalcService.AddValues(new int[] { 5, 10, 15 }).Value);
        Console.ReadLine();
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我在我的电脑上运行客户端,这可以正常工作.如果我在同事的PC上运行客户端,我会在客户端上获得此异常堆栈:

未处理的异常:System.ServiceModel.Security.MessageSecurityException:HTTP请求未经授权,客户端身份验证方案为"Negotiate".从服务器接收到的认证标头是"协商oX0we6ADCgEBonQEcm BwBgkqhkiG9xIBAgIDAH5hMF + gAwIBBaEDAgEepBEYDzIwMTAwMzExMTAzNjAzWqUFAgMEDFimAwIBKakYGxZJTlRSQU5FVC5CQVJDQVBJTlQuQ09NqhowGK ADAgEBoREw​​DxsNTERORFdNMjg2MzgwJA ==".---> System.Net.WebException:远程服务器返回错误:(401)Unauthoriz ed.---> System.ComponentModel.Win32Exception:System.Net.NTAuthentication.GetOutgoingBlob(Byte [] incomingBlob,Boolean throwOnError,SecurityStatus&statusCode)中的目标主体名称不正确

System.Net.NegotiateClient.Authenticate的System.Net.NegotiateClient.DoAuthenticate(String challenge,WebRequest webRequest,ICredentials凭证,Boole an preAuthenticate)上的System.Net.NTAuthentication.GetOutgoingBlob(String incomingBlob)处于字符串挑战(String challenge,WebRequest webRequest,ICredentials)在System.Net上的System.Net.AuthenticationManager.Authenticate(字符串质询,WebRequest请求,ICredentials凭证)处的System.Net.AuthenticationState.AttemptAuthenticate(HttpWebRequest httpWebRequest,ICredentials authInfo)处于System.Net的System.Net.HttpWebRequest.CheckResubmitForAuth()处. HttpWebRequest.CheckResubmit(Exception&e)---内部异常堆栈跟踪的结束---在System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeou t)的System.Net.HttpWebRequest.GetResponse()处 - - 内部异常堆栈跟踪结束---

任何帮助,赞赏,

谢谢,

缺口

Kwa*_*wal 8

在客户端的配置中,您需要在"端点"元素中添加一个额外元素,以指定运行该服务的主体.这是一个例子:

<identity>
      <servicePrincipalName value="user@mydomain.com" />
</identity>
Run Code Online (Sandbox Code Playgroud)

这应该允许在客户端和主机之间建立通道时进行正确的身份验证.如果服务是在另一个帐户下运行,则需要相应地调整该值(即,如果它使用系统帐户作为Windows服务运行,则类似于'host/mymachine.mydomain.com').