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