WCF与windows身份验证问题

本文关键字:问题 身份验证 windows WCF | 更新日期: 2023-09-27 18:16:33

我正在构建wcf应用程序,该应用程序仅适用于具有此wcf所在窗口的用户+密码的用户。这意味着我的wcf位于服务器X上,我调用函数GetData(5)我将看到登录表单(在windows中相同的用户)并输入用户+密码然后获取数据,我的主要目标是通过用户+密码以避免此登录窗口,但现在我不能强迫我的wcf请求windows身份验证,它正在返回数据给每个人。

我做错了什么?

我正在使用Vs2012(4.5)

p。如果有人有WCF使用Windows身份验证的例子我很高兴看到它。

我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>

WCF与windows身份验证问题

要将其他windows凭据传递给服务,请使用以下代码:

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();

这里有一个配置webHttpBinding和Windows Transport安全性。您必须更改服务名称,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>