[英]Simple WCF Authentication with windows username/password
从一侧看,我们有一台具有简单WCF服务的PC(称为“ PC_A”),该PC位于控制台应用程序上。 另一方面,我们有一个带有简单客户端(控制台/ winform / wpf)的“ PC_B”。
“ PC_A”具有一些Windows用户(用户名/密码):test_user / 123,admin / admin。
任务是:从客户端设置主机的ip / port,然后输入“ 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);
}
}
嗯,客户端没有配置:
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.