none
Proxy WCF service in self hosted application RRS feed

  • Question

  • I created a console application that acts as proxy for STS for locally running applications. The real STS hosted in IIS. When application requests security token it comes to my proxy first, then proxy resends the same request to the IIS but get the error:

    An exception of type 'System.ServiceModel.ProtocolException' occurred in mscorlib.dll but was not handled in user code Additional information: The SOAP action specified on the message,

    '"http://docs.oasis-open.org/ws-sx/ws-trust/200512/RST/Issue"',

    does not match the action specified on the HttpRequestMessageProperty,

    'http://docs.oasis-open.org/ws-sx/ws-trust/200512/RST/Issue'.

    In  EndTrust13Issue method

    Here is the proxy code:

        public class MyStsService : IWSTrust13AsyncContract
        {
            private readonly IWSTrust13AsyncContract _proxy;
    
            public MyStsService()
            {
                var wsHttpBinding = new WSHttpBinding(SecurityMode.TransportWithMessageCredential)
                {
                    Security =
                    {
                        Transport = new HttpTransportSecurity
                        {
                            ClientCredentialType = HttpClientCredentialType.None
                        },
                        Message = new NonDualMessageSecurityOverHttp()
                        {
                            ClientCredentialType = MessageCredentialType.UserName,
                            EstablishSecurityContext = false
                        }
                    }
                };
    
                var factory = new ChannelFactory<IWSTrust13AsyncContract>(wsHttpBinding);
                factory.Credentials.UserName.UserName = "XXXXX";
                factory.Credentials.UserName.Password = "ZZZZZ";
                var address = new EndpointAddress("https://copmanyXYZ.com/sts");
                _proxy = factory.CreateChannel(address);
                
            }
    
            public IAsyncResult BeginTrust13Issue(Message request, AsyncCallback callback, object state)
            {
                return _proxy.BeginTrust13Issue(request, callback, state);
            }
    
            public Message EndTrust13Issue(IAsyncResult result)
            {
                return _proxy.EndTrust13Issue(result);
    
            }
    
            public IAsyncResult BeginTrust13Cancel(Message request, AsyncCallback callback, object state)
            {
                throw new NotImplementedException();
            }
    
            public IAsyncResult BeginTrust13CancelResponse(Message request, AsyncCallback callback, object state)
            {
                throw new NotImplementedException();
            }
    
            public IAsyncResult BeginTrust13IssueResponse(Message request, AsyncCallback callback, object state)
            {
                throw new NotImplementedException();
            }
    
            public IAsyncResult BeginTrust13Renew(Message request, AsyncCallback callback, object state)
            {
                throw new NotImplementedException();
            }
    
            public IAsyncResult BeginTrust13RenewResponse(Message request, AsyncCallback callback, object state)
            {
                throw new NotImplementedException();
            }
    
            public IAsyncResult BeginTrust13Validate(Message request, AsyncCallback callback, object state)
            {
                throw new NotImplementedException();
            }
    
            public IAsyncResult BeginTrust13ValidateResponse(Message request, AsyncCallback callback, object state)
            {
                throw new NotImplementedException();
            }
    
            public Message EndTrust13Cancel(IAsyncResult ar)
            {
                throw new NotImplementedException();
            }
    
            public Message EndTrust13CancelResponse(IAsyncResult ar)
            {
                throw new NotImplementedException();
            }
    
    
            public Message EndTrust13IssueResponse(IAsyncResult ar)
            {
                throw new NotImplementedException();
            }
    
            public Message EndTrust13Renew(IAsyncResult ar)
            {
                throw new NotImplementedException();
            }
    
            public Message EndTrust13RenewResponse(IAsyncResult ar)
            {
                throw new NotImplementedException();
            }
    
            public Message EndTrust13Validate(IAsyncResult ar)
            {
                throw new NotImplementedException();
            }
    
            public Message EndTrust13ValidateResponse(IAsyncResult ar)
            {
                throw new NotImplementedException();
            }
        }

    Tuesday, December 6, 2016 4:53 PM

Answers

  • The solution is re-create a message like this

            public IAsyncResult BeginTrust13Issue(Message request, AsyncCallback callback, object state)
            {
                return _proxy.BeginTrust13Issue(CreateRstMessage3(request), callback, state);
            }
    
            private Message CreateRstMessage3(Message sourceMessage)
            {
                var xdr = sourceMessage.GetReaderAtBodyContents();
                var body = XElement.Load(xdr.ReadSubtree());
    
                MemoryStream ms = new MemoryStream();
                XmlWriter xw = XmlWriter.Create(ms);
                body.Save(xw);
                xw.Flush();
                xw.Close();
    
                ms.Position = 0;
                var xr = XmlReader.Create(ms);
    
                var message = Message.CreateMessage(_messageVersion, sourceMessage.Headers.Action, xr);
                return message;
            }

    then message is sent without error.


    Wednesday, December 7, 2016 3:19 PM

All replies

  • Hi Michael_M_1980,

    >> When application requests security token it comes to my proxy first, then proxy resends the same request to the IIS but get the error

    If you create request directly from console app proxy, will you get the same error?

    I suggest you configure Tracing to check what is error in server side.

    If possible, I would suggest you share us your sample STS Service and console app proxy which could reproduce your issue through OneDrive, and then we could try to make a test at our side.

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 7, 2016 2:32 AM
  • The solution is re-create a message like this

            public IAsyncResult BeginTrust13Issue(Message request, AsyncCallback callback, object state)
            {
                return _proxy.BeginTrust13Issue(CreateRstMessage3(request), callback, state);
            }
    
            private Message CreateRstMessage3(Message sourceMessage)
            {
                var xdr = sourceMessage.GetReaderAtBodyContents();
                var body = XElement.Load(xdr.ReadSubtree());
    
                MemoryStream ms = new MemoryStream();
                XmlWriter xw = XmlWriter.Create(ms);
                body.Save(xw);
                xw.Flush();
                xw.Close();
    
                ms.Position = 0;
                var xr = XmlReader.Create(ms);
    
                var message = Message.CreateMessage(_messageVersion, sourceMessage.Headers.Action, xr);
                return message;
            }

    then message is sent without error.


    Wednesday, December 7, 2016 3:19 PM