簡體   English   中英

本地托管的WCF服務-我不希望客戶端進行身份驗證

[英]Locally Hosted WCF service - I don't want clients to have to authenticate

我有一個由桌面應用程序托管的自托管WCF服務。
我可以在PC上本地成功連接到服務,但是至少在沒有提供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!");

我收到以下異常:

System.ServiceModel.Security.SecurityNegotiationException:服務器已拒絕客戶端憑據。 ---> System.Security.Authentication.InvalidCredentialException:服務器已拒絕客戶端憑據。 ---> System.ComponentModel.Win32Exception:登錄嘗試失敗

現在,從其他研究中,我發現可以使用以下代碼在客戶端調用中提供憑據:

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!");

現在,代碼已成功完成。

我很難弄清楚如何刪除此要求。 我嘗試弄亂客戶端上的綁定設置沒有成功。

誰能指出我正確的方向?

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>

在綁定上設置適當的身份驗證。

ClientAuthenticationType =“ None”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM