繁体   English   中英

WCF服务中的用户名身份验证不起作用

[英]Username authentication in WCF service not working

因此,我对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属性。 关于如何进行并获得简单身份验证方案的任何想法?

这可能发布得晚了,但是我遇到了同样的问题,事实证明我的验证器类逻辑做得不好。 这是我的解决方案

 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");
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM