C# wcf强类型FaultContract在UserNamePasswordValidator中不工作
在服务器端:C# wcf强类型FaultContract在UserNamePasswordValidator中不工作,c#,wcf,faultcontract,C#,Wcf,Faultcontract,在服务器端: public class Authenticator : UserNamePasswordValidator { public override void Validate(string userName, string password) { if (!AccessControlManager.ValidateUser(userName, password)) { LoginIncorrectFault fa
public class Authenticator : UserNamePasswordValidator
{
public override void Validate(string userName, string password)
{
if (!AccessControlManager.ValidateUser(userName, password))
{
LoginIncorrectFault fault = new LoginIncorrectFault();
fault.Message = "Invalid user and/or password";
throw new FaultException<LoginIncorrectFault>(fault);
};
}
}
在客户端:
try
{
var clientCredentials = new ClientCredentials();
clientCredentials.UserName.UserName = textEdit_UserName.Text;
clientCredentials.UserName.Password = textEdit_UserPwd.Text;
factory.Endpoint.Behaviors.Remove(typeof(ClientCredentials));
factory.Endpoint.Behaviors.Add(clientCredentials);
GlobalsManager.ServiceClient = factory.CreateChannel();
GlobalsManager.ServiceClient.Login();
}
catch (FaultException<LoginIncorrectFault> ex)
{
MessageBox.Show(ex.Detail.Message);
};
试试看
{
var clientCredentials=新的clientCredentials();
clientCredentials.UserName.UserName=textEdit_UserName.Text;
clientCredentials.UserName.Password=textEdit\u UserPwd.Text;
factory.Endpoint.Behaviors.Remove(typeof(ClientCredentials));
factory.Endpoint.Behaviors.Add(clientCredentials);
GlobalsManager.ServiceClient=factory.CreateChannel();
GlobalsManager.ServiceClient.Login();
}
捕获(FaultException-ex)
{
MessageBox.Show(例如Detail.Message);
};
但是,出现了错误:“从另一方收到了未安全或安全性不正确的故障。有关故障代码和详细信息,请参阅内部故障例外。”
但是,如果我在ServiceContract中运行如下抛出代码,则一切正常:
public class ESFService : IESFService
{
public void Login()
{
LoginIncorrectFault fault = new LoginIncorrectFault();
fault.Message = "Invalid user and/or password";
throw new FaultException<LoginIncorrectFault>(fault);
}
公共类ESFService:IESFService
{
公共无效登录()
{
LoginCorrectFault=新的LoginCorrectFault();
fault.Message=“无效的用户和/或密码”;
抛出新的FaultException(fault);
}
}
谢谢
更新-跟踪详细信息:
DataItem>
<TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Warning">
<TraceIdentifier>http://msdn.microsoft.com/en-US/library/System.ServiceModel.Security.SecurityBindingVerifyIncomingMessageFailure.aspx</TraceIdentifier>
<Description>The security protocol cannot verify the incoming message.</Description>
<AppDomain>EngineHost.vshost.exe</AppDomain>
<ExtendedData xmlns="http://schemas.microsoft.com/2006/08/ServiceModel/SecurityProtocolTraceRecord">
<SecurityProtocol>System.ServiceModel.Security.SymmetricSecurityProtocol</SecurityProtocol>
<Action>http://schemas.xmlsoap.org/ws/2005/02/trust/RST/SCT</Action>
<To>http://localhost:8070/ESFService</To>
<EndpointReference xmlns="http://www.w3.org/2005/08/addressing">
<Address>http://www.w3.org/2005/08/addressing/anonymous</Address>
</EndpointReference>
<MessageId>urn:uuid:cf27ff49-96b5-4d9f-bcfb-47c1d39503de</MessageId>
</ExtendedData>
DataItem>
http://msdn.microsoft.com/en-US/library/System.ServiceModel.Security.SecurityBindingVerifyIncomingMessageFailure.aspx
安全协议无法验证传入消息。
EngineHost.vshost.exe
System.ServiceModel.Security.SymmetricSecurity协议
http://schemas.xmlsoap.org/ws/2005/02/trust/RST/SCT
http://localhost:8070/ESFService
http://www.w3.org/2005/08/addressing/anonymous
urn:uuid:cf27ff49-96b5-4d9f-bcfb-47c1d39503de
更新:附加ServiceModel配置
<system.serviceModel>
<diagnostics>
<messageLogging logEntireMessage="true" logKnownPii="true" logMalformedMessages="true"
logMessagesAtTransportLevel="true" />
<endToEndTracing activityTracing="true" messageFlowTracing="true" />
</diagnostics>
<bindings>
<wsHttpBinding>
<binding name="wsHttpEndpointBinding" closeTimeout="00:10:00"
openTimeout="00:10:00" sendTimeout="00:10:00" maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<reliableSession enabled="true" />
<security mode="Message">
<message clientCredentialType="UserName" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="NewBehavior0">
<dataContractSerializer maxItemsInObjectGraph="2147483647" />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="wsHttpBindingBehaviour">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
<dataContractSerializer maxItemsInObjectGraph="2147483647" />
<serviceCredentials>
<serviceCertificate findValue="CN=tempCertServer" />
<userNameAuthentication userNamePasswordValidationMode="Custom"
customUserNamePasswordValidatorType="ESF.Runtime.AccessControlManagement.Authenticator, AccessControlManagement" />
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="wsHttpBindingBehaviour" name="ESF.Runtime.ServiceManagement.ESFService">
<endpoint binding="wsHttpBinding" bindingConfiguration="wsHttpEndpointBinding"
name="wsHttpEndpoint" contract="ESF.Runtime.ServiceContracts.IESFService" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8070/ESFService" />
<add baseAddress="net.tcp://localhost:8071" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
让您的计算机精确搜索错误消息从另一方接收到一个不安全或安全性不正确的故障。有关故障代码和详细信息,请参见内部故障异常
。这与您的验证器无关,而是应用于传输消息凭据的安全性。有很多关于这个错误的帖子。e、 g.请参阅此,请参阅跟踪详细信息。但正如我所说的,如果我在合同中加入sme,这是可行的。谢谢。您能发布您的servicemodel配置吗?当然,请参阅更新。您是否已准确搜索了错误消息,从另一方收到了不安全或安全性不正确的故障。有关故障代码和详细信息,请参见内部故障异常
。这与您的验证器无关,而是应用于传输消息凭据的安全性。有很多关于这个错误的帖子。e、 g.请参阅此,请参阅跟踪详细信息。但正如我所说的,如果我在合同中加入sme,这是可行的。谢谢。你能发布你的servicemodel配置吗?当然,请查看更新。
<system.serviceModel>
<diagnostics>
<messageLogging logEntireMessage="true" logKnownPii="true" logMalformedMessages="true"
logMessagesAtTransportLevel="true" />
<endToEndTracing activityTracing="true" messageFlowTracing="true" />
</diagnostics>
<bindings>
<wsHttpBinding>
<binding name="wsHttpEndpointBinding" closeTimeout="00:10:00"
openTimeout="00:10:00" sendTimeout="00:10:00" maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<reliableSession enabled="true" />
<security mode="Message">
<message clientCredentialType="UserName" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="NewBehavior0">
<dataContractSerializer maxItemsInObjectGraph="2147483647" />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="wsHttpBindingBehaviour">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
<dataContractSerializer maxItemsInObjectGraph="2147483647" />
<serviceCredentials>
<serviceCertificate findValue="CN=tempCertServer" />
<userNameAuthentication userNamePasswordValidationMode="Custom"
customUserNamePasswordValidatorType="ESF.Runtime.AccessControlManagement.Authenticator, AccessControlManagement" />
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="wsHttpBindingBehaviour" name="ESF.Runtime.ServiceManagement.ESFService">
<endpoint binding="wsHttpBinding" bindingConfiguration="wsHttpEndpointBinding"
name="wsHttpEndpoint" contract="ESF.Runtime.ServiceContracts.IESFService" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8070/ESFService" />
<add baseAddress="net.tcp://localhost:8071" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>