WCF can share ASP.. Net session. Different WCF client proxy classes can access the same ASP. NET session in per call mode. net session. however, the session and ASP. net sessions are different.
- The session of WCF represents a service instance, which is initialized when accessed by the client proxy class. WCF relies on message channels, secure calls, and message modes to contact sessions.
- ASP. NET sessions are similar to server-side data storage models. It maintains the session through the client cookie and Uri.
1. Use ASP. Net session for authentication and record user authentication information in aspx:
String userid = httpcontext. Current. User. Identity. Name; <br/> int timeout = httpcontext. Current. session. Timeout
2. If the WCF server can share ASP. NET sessions, you must make the following settings in the WCF configuration file:
<Servicehostingenvironment aspnetcompatibilityenabled = "true"/>3. Create a user class on the server:
Public static class userhelper <br/>{< br/> Public const string sessionuserkey = "session_current_user "; </P> <p> // <summary> <br/> // obtain the current user information <br/> /// </Summary> <br/> Public static userinfo currentuser <br/>{< br/> Get <br/>{< br/> If (! Httpcontext. current. user. identity. isauthenticated) <br/>{< br/> httpcontext. current. response. redirect (system. configuration. configurationmanager. appsettings ["logoutredirecturl"]); <br/> return NULL; <br/>}</P> <p> string key = sessionuserkey; <br/> Object User = NULL; </P> <p> If (httpcontext. current. session! = NULL) <br/> User = httpcontext. current. session [Key]; </P> <p> If (user = NULL) <br/>{< br/> var info = new adminservice (). getuserbyid (httpcontext. current. user. identity. name); // obtain user information <br/> httpcontext. current. session [Key] = Info; <br/> return Info; <br/>}< br/> return (userinfo) user; <br/>}< br/> set {httpcontext. current. session [sessionuserkey] = value ;}< br/>}< br/>}4. You can use the shared session on the WCF server to determine the permission:
[Serializable] <br/> [aspnetcompatibilityrequirements (requirementsmode = aspnetcompatibilityrequirementsmode. allowed)] <br/> [servicebehavior (instancecontextmode = instancecontextmode. percall)] <br/> Public partial class adminservice: iadminservice <br/>{< br/> Public adminservice () <br/>{< br/>}< br/> Public String insertadmin (Admin admin) <br/>{< br/> int errorcount = 0; <br/> If (! Userhelper. currentuser = NULL) <br/>{< br/> customerfaultexception exception = new customerfaultexception <br/>{< br/> exceptioncode = "s001 ", <br/> exceptionmessage = "You are not authorized to access the service. Please log on! "<Br/>}; <br/> throw new faultexception <customerfaultexception> (exception); <br/>}< br/> adminsummary = admindal. getadminsummary (Admin. userid, out errorcount); <br/> If (adminsummary = NULL) <br/> return admindal. insertadmin (Admin); <br/> else <br/> return "1001"; <br/>}5. Obtain the sessionid on the client. We can use cookies to obtain the sessionid of the client.
Void initialinvoke () <br/>{< br/> ihelloservice proxy = factory. createchannel (); <br/> using (New operationcontextscope (icontextchannel) proxy) <br/>{< br/> console. writeline (proxy. greet ("hello"); <br/> httpresponsemessageproperty responseproperty = operationcontext. current. incomingmessageproperties [httpresponsemessageproperty. name] <br/> As httpresponsemessageproperty; <br/> helper = httpsessioncookiehelper. create (string) responseproperty. headers [httpresponseheader. setcookie]); <br/>}</P> <p> (iclientchannel) proxy ). close (); <br/>}Httpsessioncookiehelper obtains the sessionid from the cookie (cookiename is "ASP. net_sessionid ")
Class httpsessioncookiehelper <br/>{< br/> const string aspnetsessionidcookiename = "ASP. net_sessionid "; <br/> string aspnetsessionid = string. empty; <br/> httpsessioncookiehelper () <br/>{}</P> <p> Public static httpsessioncookiehelper create (string cookiestring) <br/>{< br/> httpsessioncookiehelper helper = new httpsessioncookiehelper (); <br/> helper. parsecookiestring (cookiestring); <br/> return helper; <br/>}</P> <p> Public static httpsessioncookiehelper createfromsessionid (string sessionid) <br/>{< br/> httpsessioncookiehelper helper = new httpsessioncookiehelper (); <br/> helper. aspnetsessionid = sessionid; <br/> return helper; <br/>}</P> <p> Public void addsessionidtorequest (httprequestmessageproperty requestproperty) <br/>{< br/> If (string. isnullorempty (this. aspnetsessionid) <br/> return; </P> <p> string sessioncookiestring = string. format ("{0} = {1}", aspnetsessionidcookiename, this. aspnetsessionid); <br/> string cookiestring = (string) requestproperty. headers [httprequestheader. cookie]; <br/> If (string. isnullorempty (cookiestring) <br/>{< br/> cookiestring = sessioncookiestring; <br/>}< br/> else <br/>{< br/> cookiestring = string. format ("{0 };{ 1}", cookiestring, sessioncookiestring); <br/>}</P> <p> requestproperty. headers [httprequestheader. cookie] = cookiestring; <br/>}</P> <p> void parsecookiestring (string cookiestring) <br/>{< br/> If (string. isnullorempty (cookiestring) <br/> return; </P> <p> string [] cookies = cookiestring. split (';'); <br/> for (INT I = 0; I <cookies. length; I ++) <br/>{< br/> string [] cookienamevalues = Cookies [I]. split ('='); <br/> If (cookienamevalues [0] = aspnetsessionidcookiename) <br/>{< br/> This. aspnetsessionid = cookienamevalues [1]; <br/> return; <br/>}</P> <p> Public String aspnetsessionid <br/>{< br/> Get <br/> {<br/> return this. aspnetsessionid; <br/>}< br/>}
In this way, we can obtain the sessionid in the response of the first call.
Void invoke2 () <br/>{< br/> ihelloservice proxy = factory. createchannel (); <br/> using (New operationcontextscope (icontextchannel) proxy) <br/>{< br/> httprequestmessageproperty requestproperty = new httprequestmessageproperty (); <br/> helper. addsessionidtorequest (requestproperty); <br/> operationcontext. current. outgoingmessageproperties [httprequestmessageproperty. name] = requestproperty; <br/> console. writeline (proxy. greet ("howdy"); <br/>}</P> <p> (iclientchannel) proxy ). close (); </P> <p> If (interlocked. increment (ref completedcount) = 2) <br/>{< br/> waithandle. set (); <br/>}< br/>}Note: 5th points can be referred to: http://blogs.msdn.com/ B /wenlong/archive/2010/02/21/using-asp-net-sessions-from-wcf.aspx