从一侧我们有一台PC(称为“PC_A”),它有一个简单的WCF服务,托管在控制台应用程序上。 另一方面,我们有一个带有简单客户端的“PC_B”(console / winform / wpf)。
“PC_A”有一些Windows用户(用户名/密码):test_user / 123,admin / admin。
任务是:从客户端设置主机的ip /端口,并输入“PC_B”用户之一的用户名/密码。如果用户名/密码有效,则执行service的方法。
问题是:我无法理解如何进行此身份验证。我已阅读并观看指南,但仍然缺少某些内容(如何设置配置并以正确的方式发送凭据)。
希望,有人可以解释或给我一个链接到一篇文章的链接。
服务
[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);
}
}
答案 0 :(得分:0)
嗯,客户端没有配置:
var IP = "127.0.0.1";
var Port = "12345";
NetTcpBinding b = new NetTcpBinding();
b.Security.Mode = SecurityMode.Transport;
b.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows;
EndpointAddress ea = new EndpointAddress(new Uri("net.tcp://" + IP + ":" + Port + "/MyService"));
var client = new ServiceReference1.MyServiceClient(b, ea);
client.Endpoint.Address = new EndpointAddress(new Uri("net.tcp://" + IP + ":" + Port + "/MyService"));
client.ClientCredentials.Windows.ClientCredential.UserName = TB_UserName.Text;
client.ClientCredentials.Windows.ClientCredential.Password = TB_Password.Text;