Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.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服务-I don';我不希望客户端必须进行身份验证_C#_Wcf_Self Hosting - Fatal编程技术网

C# 本地托管的WCF服务-I don';我不希望客户端必须进行身份验证

C# 本地托管的WCF服务-I don';我不希望客户端必须进行身份验证,c#,wcf,self-hosting,C#,Wcf,Self Hosting,我有一个由桌面应用程序托管的自托管WCF服务。 我可以在电脑上本地成功连接到该服务,但我无法远程使用该服务,至少在不提供windows/域级别凭据的情况下是这样 我使用以下代码启动应用程序中的服务: ServiceHost host = new ServiceHost( typeof (SMService), new Uri("net.tcp://localhost:" + SMGlobals._DEFAULTSERVICEPORT.ToStrin

我有一个由桌面应用程序托管的自托管WCF服务。
我可以在电脑上本地成功连接到该服务,但我无法远程使用该服务,至少在不提供windows/域级别凭据的情况下是这样

我使用以下代码启动应用程序中的服务:

ServiceHost host = new ServiceHost(
            typeof (SMService),
            new Uri("net.tcp://localhost:" + SMGlobals._DEFAULTSERVICEPORT.ToString() + "/SMService"));

        host.AddServiceEndpoint(
            typeof(ISMService),
            new NetTcpBinding(),
            "");
        System.ServiceModel.Channels.Binding mexBinding = MetadataExchangeBindings.CreateMexTcpBinding();

        var metadataBehavior =
            new ServiceMetadataBehavior();
        host.Description.Behaviors.Add(metadataBehavior);

        host.AddServiceEndpoint(
            typeof(IMetadataExchange),
            mexBinding,
            "net.tcp://localhost:" + SMGlobals._DEFAULTSERVICEPORT.ToString() + "/SMService/mex");

        host.Open();

        SMGlobals.SMServiceHost = host;
如果我使用以下代码创建一个简单的客户端来调用服务:

var client = new SMServiceClient();
        var uri = "net.tcp://192.168.11.10:8760/SMService";
        client.Endpoint.Address = new EndpointAddress(uri);


        var initiateResponse = client.InitiateAuthentication(new InitiateAuthenticationRequest());

        MessageBox.Show("Success!");
var client = new SMServiceClient();
        var uri = "net.tcp://192.168.11.10:8760/SMService";
        client.Endpoint.Address = new EndpointAddress(uri);

        client.ClientCredentials.Windows.ClientCredential.Domain = "domain";
        client.ClientCredentials.Windows.ClientCredential.UserName = "my_user_name";
        client.ClientCredentials.Windows.ClientCredential.Password = "my_password";

        var initiateResponse = client.InitiateAuthentication(new InitiateAuthenticationRequest());

        MessageBox.Show("Success!");
我收到以下例外情况:

System.ServiceModel.Security.SecurityNegotiationException:服务器已拒绝客户端凭据。-->System.Security.Authentication.InvalidCredentialException:服务器已拒绝客户端凭据。-->System.ComponentModel.Win32异常:登录尝试失败

现在,通过其他研究,我发现我可以使用以下代码为客户端调用提供凭据:

var client = new SMServiceClient();
        var uri = "net.tcp://192.168.11.10:8760/SMService";
        client.Endpoint.Address = new EndpointAddress(uri);


        var initiateResponse = client.InitiateAuthentication(new InitiateAuthenticationRequest());

        MessageBox.Show("Success!");
var client = new SMServiceClient();
        var uri = "net.tcp://192.168.11.10:8760/SMService";
        client.Endpoint.Address = new EndpointAddress(uri);

        client.ClientCredentials.Windows.ClientCredential.Domain = "domain";
        client.ClientCredentials.Windows.ClientCredential.UserName = "my_user_name";
        client.ClientCredentials.Windows.ClientCredential.Password = "my_password";

        var initiateResponse = client.InitiateAuthentication(new InitiateAuthenticationRequest());

        MessageBox.Show("Success!");
现在,代码成功地完成了

我很难想出如何删除这个要求。我尝试过在客户端上乱搞绑定设置,但没有成功

有人能给我指出正确的方向吗?

ClientAuthenticationType=“无”


ClientAuthenticationType=“None”

Tcp绑定在默认情况下启用了安全性,因此要获得所需的内容,需要显式关闭它。像这样添加端点。您还可以浏览NetTcpBinding的MSDN帮助,因为您可能希望使用替代构造函数来关闭可靠的消息传递

 host.AddServiceEndpoint(
            typeof(ISMService),
            new NetTcpBinding(SecurityMode.None),
            "");

Tcp绑定在默认情况下启用了安全性,因此要获得所需的内容,需要显式地将其关闭。像这样添加端点。您还可以浏览NetTcpBinding的MSDN帮助,因为您可能希望使用替代构造函数来关闭可靠的消息传递

 host.AddServiceEndpoint(
            typeof(ISMService),
            new NetTcpBinding(SecurityMode.None),
            "");

我也有类似的问题。在两个进程之间本地工作,但当两个进程放在不同的机器上时(或在本地使用解析到本地机器的公共URL,例如mylocalmachine.corp.com),相同的代码失败。我发现我需要显式地将匿名绑定的安全性设置为“无”:

<binding name="TcpAnonymousBinding" portSharingEnabled="true" receiveTimeout="24:00:00">
    <security mode="None"/>
</binding>

我也有类似的问题。在两个进程之间本地工作,但当两个进程放在不同的机器上时(或在本地使用解析到本地机器的公共URL,例如mylocalmachine.corp.com),相同的代码失败。我发现我需要显式地将匿名绑定的安全性设置为“无”:

<binding name="TcpAnonymousBinding" portSharingEnabled="true" receiveTimeout="24:00:00">
    <security mode="None"/>
</binding>


是的,就是这样。。不知道我怎么会错过!谢谢,谢谢你的帮助。是的,就是这样。。不知道我怎么会错过!谢谢,谢谢你的帮助。