0
從一側我們有一臺PC(稱爲「PC_A」),它具有託管在控制檯應用程序上的簡單WCF服務。 從另一方面,我們有一個簡單的客戶端(控制檯/ winform/wpf)「PC_B」。使用Windows用戶名/密碼的簡單WCF身份驗證
「PC_A」有一些windows用戶(用戶名/密碼):test_user/123,admin/admin。
任務是:從客戶端設置主機的IP /端口,並輸入用戶名/密碼之一「PC_B」的用戶。如果用戶名/密碼有效,則執行服務的方法。
問題是:我無法理解如何進行此身份驗證。我已經閱讀並觀看指南,但仍然缺少一些東西(如何以正確的方式設置配置和發送憑證)。
希望有人能夠解釋或給我一個鏈接到一個文章,它已被解釋。
服務:
[ServiceContract]
public interface IMyService
{
[OperationContract]
string GetHddInfo(int val);
}
public class Service1 : IMyService
{
public string DoWork(int val)
{
return (string.Format("You entered: {0}", val.ToString()));
}
}
主持人:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="MyBindingSettings">
<security mode="TransportWithMessageCredential">
<transport clientCredentialType="Windows"></transport>
</security>
</binding>
</netTcpBinding>
</bindings>
<services>
<service name="Service1.Service1" behaviorConfiguration="mexBehavior">
<endpoint address="Service1" binding="netTcpBinding" contract="Service1.IMyService"></endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/"/>
<add baseAddress="net.tcp://localhost:12345/"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="mexBehavior">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
class Program
{
static void Main()
{
using (var host = new ServiceHost(typeof(Service1.Service1)))
{
host.Open();
Console.WriteLine("Host is running...");
Console.ReadLine();
}
}
}
客戶:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_IMyService" />
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://localhost:12345/Service1" binding="netTcpBinding"
bindingConfiguration="NetTcpBinding_IMyService" contract="ServiceReference1.IMyService"
name="NetTcpBinding_IMyService">
<identity>
<userPrincipalName value="Alexander-ПК\Alexander" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>
private void button1_Click(object sender, EventArgs e)
{
var IP = "127.0.0.1";
var Port = "12345";
var client = new ServiceReference1.Service1Client("NetTcpBinding_IMyService");
client.Endpoint.Address = new EndpointAddress(new Uri("net.tcp://" + IP + ":" + Port + "/Service1"));
//client.ClientCredentials.UserName.UserName = "test_user";
//client.ClientCredentials.UserName.Password = "123";
try
{
listBox1.Items.Add(client.DoWork());
}
catch (Exception ex)
{
listBox1.Items.Add(ex.Message);
}
}