WCF & # 8594; ASMX and cookies

I have a web application that communicates with a WCF service through a WCF client. At the time my code is called, authentication cookies were issued and I have a dependency on the ASMX service that expects these cookies for authentication.

I need to transfer cookies from a web application through a WCF client to a WCF service to ASMX.

Any ideas? It seems like the best option would be to set allowCookies to false, parse the cookie headers, try to recreate them in the WCF service, and then attach them to the SOAP request.

Note. I looked through this article , which seems close, but not quite applicable to this issue. In a related scenario, the ASMX service creates cookies that must be saved for subsequent ASMX service by the same WCF client.

+2
source share
1 answer

So, there are two main things that should happen:

  • Get cookie from web application context to WCF service
  • Get cookie from WCF service ASMX service

NOTE. . Since you did not specify, I assume that you are using the WCF client in your WCF service to talk to the ASMX service. If this is not the case, please let me know and I will revise this message accordingly.

Step 1:

IClientMessageInspector, , IEndpointBehavior. IClientMessageInspector:: BeforeSendRequest cookie HttpContext:: Request:: Cookies . :

public void BeforeSendRequest(ref Message request, IClientChannel channel)
{
    // Get the cookie from ASP.NET
    string cookieValue = HttpContext.Current.Request.Cookies["MyCookieName"].Value;   

    // Create a header that represents the cookie
    MessageHeader myCookieNameHeader = MessageHeader.CreateHeader("MyCookieHeaderName", "urn:my-custom-namespace", cookieValue);   

    // Add the header to the message
    request.Headers.Add(myCookieNameHeader);
}

. , cookie WCF. , WCF , . , , .

# 2:

, cookie WCF ASMX. IClientMessageInspector, , BeforeSendMessageRequest :

public void BeforeSendRequest(ref Message request, IClientChannel channel)
{
    // Get the cookie value from the custom header we sent in from step #1
    string cookieValue = OperationContext.Current.IncomingMessageHeaders.GetHeader<string>("MyCookieHeaderName", "urn:my-custom-namespace");

    HttpRequestMessageHeaderProeperty httpRequestMessageHeaderProperty;
    MessageProperties outgoingMessageProperties = OperationContext.Current.OutgoingMessageProperties;

    // Get the HttpRequestMessageHeaderProperty, if it doesn't already exist we create it now
    if(!outgoingMessageProperties.TryGetValue(HttpRequestMessageHeaderProperty.Name, out httpRequestMessageHeaderProperty))
    {
        httpRequestmessageHeaderProperty = new HttpRequestMessageHeaderProperty();

        outgoingMessageProperties.Add(HttpRequestMessageHeaderProperty.Name, httpRequestmessageHeaderProperty);
    }

    // Set the cookie header to our cookie value (note: sample assumes no other cookies set)
    httpRequestmessageHeaderProperty.Headers[HttpRequestHeader.Cookie] = cookieValue;
}

ASMX IEndpointBehavior, , , cookie .

+8

Source: https://habr.com/ru/post/1758973/


All Articles