我已经userNamePasswordValidationMode
在我的 WCF 应用程序中实现了我的自定义,如下所示:
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding>
<security mode ="Message">
<message clientCredentialType="UserName"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="True"/>
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="MyProject.Validator.MyValidator, MyProject" />
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
</system.serviceModel>
这不会引发任何错误,但是当我在客户端中引用我的服务并设置用户名/密码凭据时,即使我输入了错误的密码,我的方法仍然会被调用:
Testing.myAPIClient.client = new Testing.myAPIClient();
client.ClientCredentials.UserName.UserName = "test";
client.ClientCredentials.UserName.Password = "wrongpassword";
Console.WriteLine(client.StockQuery("123"));
Console.ReadLine();
该方法StockQuery
仍然被调用,并且代码MyValidator
甚至没有被调用:
public class MyValidator : UserNamePasswordValidator
{
public override void Validate(string userName, string password)
{
using (var ax = new AXConnector())
{
if (!(bool)ax.CallStaticClassMethod("OnlineUsers", "validateLogon", userName, password))
{
throw new UnauthorizedAccessException("Not Authorised");
}
}
}
}
编辑
这是我的 app.config:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IMyAPI" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://myServer:89/MyAPI.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IMyAPI" contract="Testing.IMyAPI"
name="BasicHttpBinding_IMyAPI" />
</client>
</system.serviceModel>
编辑 2
服务接口:
[ServiceContract]
public interface IMyAPI
{
string UserName { [OperationContract] get; [OperationContract] set; }
string Password { [OperationContract] get; [OperationContract] set; }
[OperationContract]
bool StockQuery(string partNo);
[OperationContract]
decimal PriceQuery(string partNo, string accNo);
}
服务等级:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required),
ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Single)]
public class MyAPI : IMyAPI
{
public string UserName { get; set; }
public string Password { get; set; }
public MyAPI()
{
this.CheckSecurity();
}
private void CheckSecurity()
{
if (this.UserName != "test" && this.Password != "123")
{
throw new UnauthorizedAccessException("Not Authorised");
}
}
// StockQuery and PriceQuery methods...
}