Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
367 views
in Technique[技术] by (71.8m points)

c# - Making web service calls with WCF client + cookies

Been implementing the Basic security Authentication in a WCF Service.

Managed to get the .ASPXCookie from the web service. But, how to pass along the received cookie back to the next request?

var authClient = new MovieDbClient();
using (new OperationContextScope(authClient.InnerChannel))
{
    isValid = authClient.Login("userName", "passWord*");
    if (isValid)
    {
        var response = (HttpResponseMessageProperty)OperationContext.Current.IncomingMessageProperties[HttpResponseMessageProperty.Name];
        sharedCookie = response.Headers["Set-Cookie"];
    }
}

I tried to print the SharedCookie and was successful in it. It looks something like,

".ASPXAUTH=E499CA76EAC178A96BE5CA1E314CC90E0A6F9B95AD221EF5AD7D43598E701DC034D40904DBB8ECFBFB3EA21F2597D3C8DAB9B19A0491FD5858E9F0A4B6DC6E6A980FBB4CCADE191855A029CF8236C6890BEE28665C236992632807D1021AA138; expires=Tue, 07-Jan-2014 06:22:22 GMT; path=/; HttpOnly"

The question is how do I pass this cookie information in my next request using wCF Client - authClient ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To add a Cookie header to the WCF request in the current context given that you have the cookie string already:

var prop = new HttpRequestMessageProperty();
prop.Headers.Add(HttpRequestHeader.Cookie, sharedCookie);
OperationContext.Current.OutgoingMessageProperties.Add(HttpRequestMessageProperty.Name, prop);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...