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);
}
}