asp.net - Can HttpClient share same the HttpContext.Current.Session.SessionID as its host? -
there times when want make calls httpclient within asp.net web site internal resources ( asp.net webforms , mvc running together).
the first hurdle forms auth cookie, think solved, second problem though when call mvc controller, system.web.httpcontext.current.session.sessionid different when initiated call. sessionid being used key cache items, items come null in controller.
so question boils down this, did implement cookie swapping correctly , can httpclient inherit session it's host if has one?
try { // boostrap properties tab preloading data uri baseaddress = new uri(request.url.getleftpart(uripartial.authority)); cookiecontainer cookiecontainer = new cookiecontainer(); string resource = string.format("/contact/propertybootstrapper/{0}", request.params["contactguid"]); httpcookie appcookie = request.cookies[formsauthentication.formscookiename]; using (httpclienthandler handler = new httpclienthandler { cookiecontainer = cookiecontainer }) using (httpclient client = new httpclient(handler) { baseaddress = baseaddress }) { debug.assert(appcookie != null, "appcookie != null"); cookiecontainer.add(baseaddress, new cookie(appcookie.name,appcookie.value)); httpresponsemessage response = client.getasync(resource).result; if (response.issuccessstatuscode) { var result = response.content.readasstringasync(); } } } catch (exception exception) { system.diagnostics.trace.writeline(exception); }
you same session cookie - copy context client. session cookie carries cookieid.
Comments
Post a Comment