1

在服务器端:

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

服务合约详情:

    [OperationContract]
    [FaultContract(typeof(LoginIncorrectFault))]
    void Login();

在客户端:

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

但是,出现错误:“从另一方收到不安全或不正确安全的故障。有关故障代码和详细信息,请参阅内部 FaultException。”

但是,如果我像下面这样在 ServiceContract 中运行抛出代码,一切正常:

public class ESFService : IESFService
{
    public void Login()
    {
        LoginIncorrectFault fault = new LoginIncorrectFault();

        fault.Message = "Invalid user and/or password";

        throw new FaultException<LoginIncorrectFault>(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>

更新:附加 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>
4

0 回答 0