【问题标题】:Username authentication in WCF service not workingWCF 服务中的用户名身份验证不起作用
【发布时间】:2015-09-08 23:56:41
【问题描述】:

所以我对 WCF 还是很陌生,我需要一个基本的用户名或基于证书的身份验证方案来实现一些基本的安全性,以便只允许从我的应用程序中使用该服务。无论如何,为了简短起见,这是我的配置,所以

  <system.serviceModel>
<bindings>
  <wsHttpBinding>
    <binding name="wsHttpBinding">
      <security>
        <message clientCredentialType="UserName"/>
      </security>
    </binding>
  </wsHttpBinding>
</bindings>
<client>
  <endpoint binding="wsHttpBinding" bindingConfiguration="wsHttpBinding"
    contract="uConnect.Web.IUConnectService" name="wsHttpBindingEndpoint" />
</client>
<behaviors>
  <serviceBehaviors>
    <behavior name="">
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
      <serviceCredentials>
        <serviceCertificate findValue="CN=tempCert" storeLocation="CurrentUser" />
        <userNameAuthentication userNamePasswordValidationMode="Custom"
          customUserNamePasswordValidatorType="uConnect.Web.AuthValidation, uConnect.Web" />
      </serviceCredentials>
    </behavior>
  </serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>

这是我的自定义验证类,目前使用硬编码的用户名/密码组合方法

namespace uConnect.Web
{
class AuthValidation : UserNamePasswordValidator
{
    public override void Validate(string userName, string password)
    {
        if (userName != "test" || password != "test")
            throw new SecurityException("Error: username/password combination invalid.");
    }
}
}

我已经学习了几个教程,我相信一切都配置正确。但是问题不在于该服务不起作用,它工作正常。问题是我可以在不提供用户名/密码的情况下访问我的服务合同和调用方法。但是,如果我用属性[PrincipalPermission(SecurityAction.Demand, Authenticated = true)] 装饰我的类,那么当我尝试访问它时它会崩溃。现在这可能是因为我实际上没有经过身份验证,或者完全是其他原因。但抛出的所有异常都不是我期待的SecurityException

最后一件事,到目前为止我看到的所有教程都表明您提供了用户名/密码,例如

myService.ClientCredentials.UserName.UserName = "username";
myService.ClientCredentials.UserName.Password = "p@ssw0rd";

但是当我将 WCF 合同引用添加到我的解决方案时,它作为 System.Web.Services.Protocols.SoapHttpClientProtocol 类型的类提供,它不提供属性 ClientCredentials。关于我应该如何继续并使简单的身份验证方案正常工作的任何想法?

【问题讨论】:

    标签: c# wcf authentication


    【解决方案1】:

    这可能会迟到,但我遇到了同样的问题,结果证明我的验证器类逻辑做得不太好。这是我的解决方案

     public override void Validate(string userName, string password)
        {
            if (null == userName || null == password)
            {
                throw new ArgumentNullException();
            }
    
            if (!(userName == "test1" && password == "1tset") && !(userName == "test2" && password == "2tset"))
            {
                throw new SecurityTokenException("Unknown Username or Incorrect Password");
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-26
      • 1970-01-01
      • 1970-01-01
      • 2012-04-29
      • 2018-05-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多