Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从客户端传递到WCF的凭据不在那里_C#_Wcf - Fatal编程技术网

C# 从客户端传递到WCF的凭据不在那里

C# 从客户端传递到WCF的凭据不在那里,c#,wcf,C#,Wcf,我正试图将windows凭据传递到需要windows身份验证的WCF服务中,但我的凭据似乎无法进入该服务。我的服务不会抛出任何错误,但当我检查下面2个错误时,它们都是空的。 var user=WindowsIdentity.GetCurrent().user; var callerUserName=ServiceSecurityContext.Current.WindowsIdentity.User 这是我的客户端代码 ServiceReference1.DispatchServiceClien

我正试图将windows凭据传递到需要windows身份验证的WCF服务中,但我的凭据似乎无法进入该服务。我的服务不会抛出任何错误,但当我检查下面2个错误时,它们都是空的。
var user=WindowsIdentity.GetCurrent().user;
var callerUserName=ServiceSecurityContext.Current.WindowsIdentity.User

这是我的客户端代码

ServiceReference1.DispatchServiceClient服务=新的DispatchServiceClient();
service.ClientCredentials.Windows.ClientCredentials=ServiceCredentialsManager.GetNetworkCredentials();
service.ClientCredentials.UserName.UserName=ServiceCredentialsManager.GetNetworkCredentials().UserName;
service.ClientCredentials.UserName.Password=ServiceCredentialsManager.GetNetworkCredentials().Password

客户端配置-


服务配置-

 <system.serviceModel>
      <bindings>
      <basicHttpBinding>
          <binding name="basicHttpsBinding">
              <security mode="Transport">
                  <transport clientCredentialType="Windows" />
              </security>
          </binding>
      </basicHttpBinding>
  </bindings>
<behaviors>
  <serviceBehaviors>
    <behavior>
      <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
      <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<protocolMapping>
  <add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />


请注意,为了使用Windows身份验证,服务和客户端应用程序必须在同一Windows域中运行

还请确保在客户端中指定的值不是空的。通常,在使用Windows身份验证时,无法从代码访问密码

如果使用Windows身份验证对服务客户端进行身份验证,则可能不应该手动将凭据传递给服务。身份验证过程应该由WCF自动处理,而不仅仅依赖于发送凭证,例如,它可以使用Kerberos票证

请在此处查看客户和服务的一些描述和代码示例:

更新

经过一些研究后,我发现一些来源表明,在客户端代码中适当设置凭据可以使WCF从域外进行身份验证:


这些文章中建议的代码示例类似,但与您在问题中发布的代码示例略有不同。我还没有测试这些方法,它们可能无法在所有情况下工作。

问题在于客户端位于域之外,因此我必须在客户端上传递服务用户凭据。这就是我在进行服务呼叫时设置用户凭据的原因。请查看以下帖子:。它使用与您发布的代码类似但略有不同的代码。下面是另一个类似案例的代码示例(从域外访问WCF服务):您是否配置了IIS中允许的身份验证方法?此外,我只使用过OperationContext.Current.ServiceSecurityContext来获取当前用户标识。我不确定这是否和你已经在做的事情是一样的。
 <system.serviceModel>
      <bindings>
      <basicHttpBinding>
          <binding name="basicHttpsBinding">
              <security mode="Transport">
                  <transport clientCredentialType="Windows" />
              </security>
          </binding>
      </basicHttpBinding>
  </bindings>
<behaviors>
  <serviceBehaviors>
    <behavior>
      <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
      <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<protocolMapping>
  <add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />