另一种选择是实现您自己的安全性。这是一个基本示例。
WCF 服务
在您的服务中,将ServiceBehavior 的InstanceContextMode 更改为PerSession 和ConcurrencyMode 更改为Single
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Single)]
public class SomeService : ISomeService
{
// ...
}
在您的服务中添加 Username 和 Password 属性。
public string UserName { [OperationContract] get; [OperationContract] set; }
public string Password { [OperationContract] get; [OperationContract] set; }
添加用于检查安全性的私有方法。
public void CheckSecurity()
{
if ((this.UserName == null || this.Password == null) ||
this.UserName == "username" && this.Password == "password"))
{
throw new FaultException("Unknown username or incorrect password.");
}
}
然后在你的每个服务类构造函数方法中调用CheckSecurity方法。
public SomeServiceMethod1()
{
this.CheckSecurity();
// some method codes
}
客户端应用程序
在您的客户端应用程序代码中,为每个实例设置服务用户名和密码,或创建一个静态类来为您执行此操作。
您也可以尝试在用户名和密码中使用加密来增加安全性。
请注意,这只是为您添加另一个可能满足您需求的选项,但您应该始终尝试使用 Microsoft 的做事方式。