【问题标题】:Does WCF UserNamePasswordValidator require checking PrimaryIdentity.IsAuthenticated?WCF UserNamePasswordValidator 是否需要检查 PrimaryIdentity.IsAuthenticated?
【发布时间】:2010-08-11 15:32:10
【问题描述】:

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

  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?
      }
  }

任何帮助将不胜感激。

【问题讨论】:

    标签: c# wcf validation authentication operationcontract


    【解决方案1】:

    来自本文中的几个 cmets - Silverlight 3: Securing your WCF service with a custom username / password authentication mechanism 和各种测试 - if ([...]PrimaryIdentity.IsAuthenticated) 部分不是必需的。在UserNamePasswordValidator 中抛出错误可以中止安全协商。

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

    【讨论】:

      猜你喜欢
      • 2021-11-06
      • 1970-01-01
      • 2016-11-09
      • 1970-01-01
      • 2014-05-30
      • 2013-10-16
      • 2011-08-20
      • 2011-02-21
      • 1970-01-01
      相关资源
      最近更新 更多