1. 程式人生 > >WCF Cookie處理

WCF Cookie處理

com images 技術 invoke ges 簡單 alt col log

wcf操作cookie看似很簡單,如果不對Wcf研究一把還是很難的,基本上無從下手。

這裏上一圖

技術分享

上面的結果,豈止是坑爹,簡直就是坑爹!!!

廢話不多說,直接上代碼:

cookie讀取

1             var requestProperties = OperationContext.Current.IncomingMessageProperties;
2 
3             var hrp = requestProperties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
4 5 var cookie = hrp.Headers[System.Net.HttpRequestHeader.Cookie];

cookie寫入

1             var responseProperties = OperationContext.Current.OutgoingMessageProperties;
2 
3             var httpResponseProperties = new HttpResponseMessageProperty();
4 
5             httpResponseProperties.Headers.Add(HttpResponseHeader.SetCookie, "
name=yswenli"); 6 7 responseProperties.Add(HttpResponseMessageProperty.Name, httpResponseProperties);

原理就是wcf 的 restful 是用[webget]或[webinvoke]針對operation的綁定,所以要使用web相關的就可能到OperationContext 裏面去找~

轉載請註明本文地址:http://www.cnblogs.com/yswenli/p/7274549.html

WCF Cookie處理