使用Kerberos身份验证时出现WCF Message Inspector错误

时间:2017-04-27 09:48:33

标签: c# wcf

我有一个WCF服务主机和预配置的邮件检查程序,可以按XSD架构和整体邮件大小验证邮件。这是它的实现。

public class SimpleMessageInspector : IDispatchMessageInspector
{
    private AsyncAPISender _messageSender = new AsyncAPISender();
    private ILog logger = LogManager.GetLogger(typeof(SimpleMessageInspector));
    private readonly XmlSchemaSet _schemas;
    // Max packet syze 50 Mb by default
    private const int MaxPacketSizeByDefault = 52428800;

    //Other methods of IDispatchMessageInspector

    public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
    {
        logger.DebugFormat("Recieved SOAP message: {0}", request);

        var mb = request.CreateBufferedCopy(int.MaxValue);

        request = mb.CreateMessage();
        var copyForValidation = mb.CreateMessage();
        var copyForCheckSize = mb.CreateMessage();

        ValidateMessage(ref copyForValidation);

        CheckMessageSize(mb, ref copyForCheckSize);

        return null;
    }

    void ValidateMessage(ref System.ServiceModel.Channels.Message message)
    {
        XmlDocument bodyDoc = new XmlDocument();
        bodyDoc.Load(message.GetReaderAtBodyContents().ReadSubtree());
        XmlReaderSettings settings = new XmlReaderSettings();
        settings.Schemas.Add(_schemas);
        settings.ValidationType = ValidationType.Schema;
        XmlReader r = XmlReader.Create(new XmlNodeReader(bodyDoc), settings);

        try
        {
            while (r.Read()) { }
        }
        catch (Exception e)
        {
            throw new ArgumentException("Error on validation by xsd schema", e);
        }
    }

    private void CheckMessageSize(MessageBuffer buffer, ref Message message)
    {
        int maxPacketSize;
        var maxPacketSizeFromConfig = ConfigurationManager.AppSettings["MaxPacketSize"];
        if (!Int32.TryParse(maxPacketSizeFromConfig, out maxPacketSize))
        {
            maxPacketSize = MaxPacketSizeByDefault;
        }

        if (buffer.BufferSize > maxPacketSize)
        {
            var messageInfo = GetMessageInfoType(ref message);
            if (messageInfo != null)
                _messageSender.CreateResultTask(messageInfo, null, "Max message size exceeded", false);

            throw new Exception("Max message size exceeded");
        }
    }
}

当主持人收到消息时我发现异常"此消息不支持该操作,因为它已被读取"它只有在主机使用Kerberos绑定basicHttp时才能正常运行一切正常。这是导致错误的绑定配置。

<binding name="customKerberosBinding">
  <security authenticationMode="Kerberos" allowInsecureTransport="true" enableUnsecuredResponse="false" requireDerivedKeys="false" protectTokens="false" requireSignatureConfirmation="false" messageSecurityVersion="WSSecurity10WSTrust13WSSecureConversation13WSSecurityPolicy12BasicSecurityProfile10"></security>
  <textMessageEncoding messageVersion="Soap11"></textMessageEncoding>
            <httpTransport maxReceivedMessageSize="2000000000"></httpTransport>
</binding>

有没有解释为什么只发生在kerberos绑定和可能的解决方案?

1 个答案:

答案 0 :(得分:0)

根据我使用IDispatchMessageInspector的经验,您只能阅读一次消息。由于您正在阅读它并通过引用传递,我认为这就是您收到此错误的原因。在我进行验证后的ValidateMessage方法中,我创建了一条消息并将其返回。

object IDispatchMessageInspector.AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
{
    if (validateRequest)
    {
        if (!request.IsEmpty && !request.IsFault)
        {
            request = ValidateRequestMessageBody(request);
            channel.Close();
            channel.Dispose();
        }
    }
    return null;
}

private Message ValidateRequestMessageBody(System.ServiceModel.Channels.Message message)
{
    //do stuff
    var reader = XmlReader.Create(memStream, settings);                        
    var newMessage = Message.CreateMessage(reader, int.MaxValue, message.Version);
    newMessage.Headers.Clear();
    newMessage.Headers.CopyHeadersFrom(message.Headers);
    return newMessage;
}