2

目前我有一个使用 aUserNamePasswordValidator来验证客户端用户的服务。验证代码如下:

  public override void Validate(String userName, String password)
  {
      if (userName == null) || (password == null)
          throw new FaultException("Username and/or password not specified.");
      if (userName != "test") && (password != "tset")
          throw new FaultException("Invalid username and/or password.");
  }

如您所见,当出现问题时,代码总是会抛出异常。

现在的问题 - 有什么理由我应该检查ServiceSecurityContext.Current.PrimaryIdentity.IsAuthenticated我的函数内部是否为真OperationContract?例如,

  public interface IMyService
  {
      [OperationContract]
      void myOpContract();
  }

  public class MyService : IMyService
  {
      public void myOpContract()
      {
          // Do I really need this conditional statement?
          if (ServiceSecurityContext.Current.PrimaryIdentity.IsAuthenticated)
              // Proceed as expected
          else
              // Fail?
      }
  }

任何帮助将不胜感激。

4

1 回答 1

1

从本文中的几条评论 - Silverlight 3:使用自定义用户名/密码身份验证机制和各种测试保护您的 WCF 服务 - 该if ([...]PrimaryIdentity.IsAuthenticated)部分不是必需的。在内部抛出错误可以UserNamePasswordValidator中止安全协商。

但是,代表作者的一个好主意是,if ([...]PrimaryIdentity.IsAuthenticated)如果将来添加没有安全性的新绑定(连接类型),则保留条件语句会有所帮助。

于 2010-08-15T08:06:08.743 回答