WCF服务中的用户名身份验证不起作用
本文关键字:身份验证 不起作用 用户 服务 WCF | 更新日期: 2023-09-27 18:12:01
所以我对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");
}
}