简体   繁体   中英

WCF. Use windows credentials on service side

I have client on WPF and service. It's worked with security mode=None. But now I need on service side Windows credentials of the user who call service for making some action on Azure. So I changed configuration and now it's the following:

Service

<system.serviceModel>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<client />
<bindings>
  <wsHttpBinding>
    <binding name="TrackingHoursServiceBinding" maxReceivedMessageSize="620000">
     <!-- WAS THIS 
    <security mode="None">
        <transport clientCredentialType="None" />
        <message establishSecurityContext="false" />
      </security> -->

    <!-- This is my changes  -->
      <security mode="Message">
        <message clientCredentialType="Windows" />
      </security>
    <!-- This my changes  -->
    </binding>
  </wsHttpBinding>
  <basicHttpBinding>
    <binding name="BasicHttpBinding_IPortableService" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" receiveTimeout="00:10:00" sendTimeout="00:10:00" />
  </basicHttpBinding>
</bindings>
<services>
  <service behaviorConfiguration="AAA.AAA.AAA.AAA.TrackingHoursServiceBehavior" name="AAA.AAA.AAA.AAA.TrackingHoursService">
    <endpoint address="" binding="wsHttpBinding" bindingConfiguration="TrackingHoursServiceBinding" contract="AAA.AAA.AAA.AAA.ITrackingHoursService">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
    <endpoint address="pcl" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IPortableService" contract="AAA.AAA.AAA.AAA.ITrackingHoursService" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="AAA.AAA.AAA.AAA.TrackingHoursServiceBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<extensions>
  <behaviorExtensions>
    <add name="connectionStatusBehavior" type="Microsoft.ServiceBus.Configuration.ConnectionStatusElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <add name="transportClientEndpointBehavior" type="Microsoft.ServiceBus.Configuration.TransportClientEndpointBehaviorElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <add name="serviceRegistrySettings" type="Microsoft.ServiceBus.Configuration.ServiceRegistrySettingsElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  </behaviorExtensions>
  <bindingElementExtensions>
    <add name="netMessagingTransport" type="Microsoft.ServiceBus.Messaging.Configuration.NetMessagingTransportExtensionElement, Microsoft.ServiceBus,  Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <add name="tcpRelayTransport" type="Microsoft.ServiceBus.Configuration.TcpRelayTransportElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <add name="httpRelayTransport" type="Microsoft.ServiceBus.Configuration.HttpRelayTransportElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <add name="httpsRelayTransport" type="Microsoft.ServiceBus.Configuration.HttpsRelayTransportElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <add name="onewayRelayTransport" type="Microsoft.ServiceBus.Configuration.RelayedOnewayTransportElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  </bindingElementExtensions>
  <bindingExtensions>
    <add name="basicHttpRelayBinding" type="Microsoft.ServiceBus.Configuration.BasicHttpRelayBindingCollectionElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <add name="webHttpRelayBinding" type="Microsoft.ServiceBus.Configuration.WebHttpRelayBindingCollectionElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <add name="ws2007HttpRelayBinding" type="Microsoft.ServiceBus.Configuration.WS2007HttpRelayBindingCollectionElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <add name="netTcpRelayBinding" type="Microsoft.ServiceBus.Configuration.NetTcpRelayBindingCollectionElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <add name="netOnewayRelayBinding" type="Microsoft.ServiceBus.Configuration.NetOnewayRelayBindingCollectionElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <add name="netEventRelayBinding" type="Microsoft.ServiceBus.Configuration.NetEventRelayBindingCollectionElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <add name="netMessagingBinding" type="Microsoft.ServiceBus.Messaging.Configuration.NetMessagingBindingCollectionElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  </bindingExtensions>
</extensions>
</system.serviceModel>

Client

 <system.serviceModel>
<bindings>
  <wsHttpBinding>
    <binding name="TrackingHoursServiceBinding" maxReceivedMessageSize="2147483647" useDefaultWebProxy="False">
      <readerQuotas maxDepth="32" maxStringContentLength="20000" maxArrayLength="2147483647" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
     <!-- WAS 
     <security mode="None" /> 
     -->
     <security mode="Message">
        <message clientCredentialType="Windows" />
      </security>
    </binding>
  </wsHttpBinding>
  <basicHttpBinding>
    <binding name="BasicHttpBinding_ITFSService" />
  </basicHttpBinding>
</bindings>
<client>
  <endpoint address="http://AAA/TrackingHoursService.svc" binding="wsHttpBinding" bindingConfiguration="TrackingHoursServiceBinding" contract="ITrackingHoursService" name="WSHttpBinding_ITrackingHoursService">
    <identity>
      <dns value="localhost" />
    </identity>
  </endpoint>
  <endpoint address="http://2121212/WSELFO/TFSService.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ITFSService" contract="ITFSService" name="BasicHttpBinding_ITFSService" />
</client>
</system.serviceModel>

On the service side, i try take credential using

System.Net.CredentialCache.DefaultCredentials

or

System.Net.CredentialCache.DefaultNetworkCredentials

But they return empty object.... How i can solve this issue?

After my test, I found that System.Net.CredentialCache.DefaultCredentials does not return null, whether on the client or server. Do you want to obtain username, password or domain through System.Net.CredentialCache.DefaultCredentials? If this is the case, it will return null because the instance DefaultCredentials returned by ICredentials cannot be used to view the user name, password, or domain of the current security context.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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