简体   繁体   中英

WCF with windows authentication problems

I am building wcf aplication that will work only with users that have User+Password to the windows where this wcf is located.That mean is my wcf located on server X and i call function GetData(5) i will see the logon form (the same users that in windows) and entered User+Password and then get data back, my main goal is to pass User+Password to avoid this logon window,but now i can't forse my wcf to ask for windows authentication,it is returning data to everyone.

What i am doing wrong?

I am using Vs2012(4.5)

Ps if any one have example of wcf that use windows authentication i will be very happy to see it.

My webConfig

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <authentication mode="Windows" />
    <identity impersonate="false" />
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <services>
      <service name="WcfService10.Service1">
        <endpoint address="WCF10" binding="basicHttpBinding" bindingConfiguration="NewBinding10"
          contract="WcfService10.IService1" />
      </service>
    </services>
    <bindings>
      <basicHttpBinding>
        <binding name="NewBinding10">
          <security mode="TransportCredentialOnly">
          <transport clientCredentialType="Windows" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>

    <behaviors>
      <serviceBehaviors>
        <behavior>

          <serviceCredentials>
            <userNameAuthentication userNamePasswordValidationMode="Windows" customUserNamePasswordValidatorType="Type, Assembly" />
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />



  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>

    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

Here one config with webHttpBinding and Windows Transport security. You have to change the service name, baseAddress, contract="Server.IServicemame"

<system.serviceModel>
    <!--Services-->
    <services>
        <service name="Server.servicemame">
            <host>
                <baseAddresses>
                    <add baseAddress="http://localhost:9011/servicemame/service"/>
                </baseAddresses>
            </host>
            <endpoint address="" binding="webHttpBinding" contract="Server.IServicemame" bindingConfiguration="HttpBindingWithSecurity">
                <identity>
                    <dns value="localhost"/>
                </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        </service>
    </services>
    <!--Behaviors-->
    <behaviors>
        <serviceBehaviors>
            <behavior>
                <serviceMetadata httpGetEnabled="true"/>
                <serviceDebug includeExceptionDetailInFaults="true"/>
                <serviceAuthorization impersonateCallerForAllOperations="false" />
                <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
                <serviceCredentials>
                    <windowsAuthentication allowAnonymousLogons="false"/>
                </serviceCredentials>
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <!--Bindings-->
    <bindings>
        <webHttpBinding>
            <binding name="HttpBindingWithSecurity">
                <security mode="TransportCredentialOnly">
                    <transport clientCredentialType="Windows"/>
                </security>
            </binding>

        </webHttpBinding>
    </bindings>
</system.serviceModel>

To pass alternative windows credentials to a service, use the following code:

var proxy = new MyServiceClient();
proxy.ClientCredentials.Windows.ClientCredential.Domain = "MyDomain";
proxy.ClientCredentials.Windows.ClientCredential.UserName = "MyUsername";
proxy.ClientCredentials.Windows.ClientCredential.Password = "MyPassword";
proxy.DoSomething();
proxy.Close();

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