2015-09-08 47 views
0

因此,对于WCF我很新,我需要一个基本的用户名或基于证书的身份验证方案来实现某些基本安全性,只允许我的应用程序使用该服务。不管怎么说,让事情变得很短,这是我的配置,以便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 = "[email protected]"; 

但是,当我的WCF合同引用添加到我的解决方案它作为一类提供的用户名/密码System.Web.Services.Protocols.SoapHttpClientProtocol其中不提供属性ClientCredentials。关于我应该如何继续并获得简单认证方案的任何想法?

回答

0

这可能迟到发布,但我有同样的问题,结果我的验证类逻辑没有做得很好。这里是我的解决方案

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