Autor Beitrag
jahuer1
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 39



BeitragVerfasst: Mo 28.02.11 16:22 
Ich habe einen WCF client, der Meldungen signiert und verschlüsselt an einen Webservice schickt.

Frage:
Wie kann ich die ausgehende SOAP message in folgendem Status speichern: signiert, aber nicht verschlüsselt?

Ich habe bis jetzt den folgenden Interceptor:

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
       public class myOutputMessageInspector         
    {
        internal class OutputMsgInterceptor : IClientMessageInspector, IDispatchMessageInspector
        {
            private readonly bool Online;
            private readonly bool LogMessages;
            private readonly String OutFileName;
            private readonly String InFileName;

            public OutputMsgInterceptor(String outFileName, String inFileName, bool doLog, bool isOnline)
            {
                this.Online = isOnline;
                LogMessages = doLog;
                this.OutFileName = outFileName;
                this.InFileName = inFileName;
            }

            public object BeforeSendRequest(ref Message request, IClientChannel channel)
            {
                SaveMessage(ref request, OutFileName);
                return null;
            }

            public void AfterReceiveReply(ref Message reply, object correlationState)
            {
                SaveMessage(ref reply, InFileName);
            }

            private void SaveMessage(ref Message message, string filename)
            {
                if (LogMessages && !String.IsNullOrEmpty(filename))
                {
                    // Create a buffer.
                    MessageBuffer buffer = message.CreateBufferedCopy(Int32.MaxValue);

                    // Set the request reference to an unspoiled clone.
                    message = buffer.CreateMessage();

                    // Make another unspoiled clone to process (taint) locally within this method.
                    Message originalMessage = buffer.CreateMessage();

                    // Log the SOAP xml.
                    FileStream fileStream = new FileStream(@filename, FileMode.Create);
                    StreamWriter m_streamWriter = new StreamWriter(fileStream);
                    m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
                    m_streamWriter.Write(originalMessage.ToString());
                    m_streamWriter.Flush();
                }
            }

            #region IDispatchMessageInspector Members

            public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
            {
                throw new NotImplementedException();
            }

            public void BeforeSendReply(ref Message reply, object correlationState)
            {
                throw new NotImplementedException();
            }

            #endregion
        }

        internal class OutputMsgBehavior : IEndpointBehavior
        {
            private readonly bool enabled;
            private readonly bool Online;
            private readonly bool LogMessages;
            private readonly String OutFileName;
            private readonly String InFileName;

            internal OutputMsgBehavior(bool enabled, String outFileName, String inFileName, bool doLog, bool isOnline)
            {
                this.enabled = enabled;
                this.OutFileName = outFileName;
                this.InFileName = inFileName;
                Online = isOnline;
                LogMessages = doLog;
            }

            public void Validate(ServiceEndpoint endpoint)
            {
            }

            public void AddBindingParameters(ServiceEndpoint serviceEndpoint,
                                             BindingParameterCollection bindingParameters)
            {
            }

            public void ApplyClientBehavior(
                ServiceEndpoint endpoint,
                ClientRuntime clientRuntime)
            {
                if (false == enabled)
                {
                    return;
                }
                myOutputMessageInspector.OutputMsgInterceptor interceptor =
                    new myOutputMessageInspector.OutputMsgInterceptor(OutFileName,InFileName,LogMessages,Online);
                clientRuntime.MessageInspectors.Add(interceptor);
            }

            public void ApplyDispatchBehavior(
                ServiceEndpoint endpoint,
                EndpointDispatcher endpointDispatcher)
            {
                if (false == enabled)
                {
                    return;
                }
                myOutputMessageInspector.OutputMsgInterceptor interceptor =
                    new myOutputMessageInspector.OutputMsgInterceptor(OutFileName, InFileName, LogMessages, Online);
                endpointDispatcher.DispatchRuntime.MessageInspectors.Add(interceptor);
            }


        }


    }


Angehängt wie folgt:

ausblenden C#-Quelltext
1:
2:
3:
                var factory
                = new ChannelFactory<myPort>(customBinding);
factory.Endpoint.Behaviors.Add(new myOutputMessageInspector.OutputMsgBehavior(true"Request_Decrypted.xml""Response_Decrypted.xml", LogMessages, Online));


Das security binding ist wie folgt:

ausblenden C#-Quelltext
1:
asymmetricSecurityBindingElement.MessageProtectionOrder = MessageProtectionOrder.SignBeforeEncrypt					


Grundsätzlich klappt das ganz gut. Wenn man sich nun aber "Request_Decrypted.xml" anschaut, ist die SOAP message nicht signiert (dh. unsigniert und unverschlüsselt). Hingegen ist "Response_Decrypted.xml" noch signiert, aber schon entschlüsselt.

Hat jemand einen heissen Tipp, wie ich meine SOAP request signiert bekomme, aber noch nicht verschlüsselt?

Moderiert von user profile iconChristian S.: Code- durch C#-Tags ersetzt