c# - Pass header cookies from Web API post to header location page -


i have asp.net web api posted external source. values posted web api used determine user's rights on our website. web api passes result object of business logic bunch of cookies our asp landing page. problem cookies no longer available in web page web api routed response to.

here web api:

    [httppost]     public httpresponsemessage reports(reportrequest reportrequest)     {         if (reportrequest != null)         {             var reportaccess = new switchbl().checkuseraccess(reportrequest);              var response = request.createresponse(httpstatuscode.moved);             response.headers.location = new uri(baseurl() + "/menu.aspx");              var json = jsonconvert.serializeobject(reportaccess);             dictionary<string, string> biscuittin = jsonconvert.deserializeobject<dictionary<string, string>>(json);              foreach (var biscuit in biscuittin)             {                 var cookie =                     new cookieheadervalue(biscuit.key, biscuit.value ?? "")                     {                         expires = datetimeoffset.now.adddays(1),                         domain = request.requesturi.host == "localhost" ? null : request.requesturi.host,                         httponly = true                     };                 //cookierjar.add(cookie);                 response.headers.addcookies(new cookieheadervalue[] {cookie} );             }             return response;         }         return request.createresponse(httpstatuscode.badrequest);     } 

and far simple aspx page shows count = 0:

public partial class menu : system.web.ui.page {     protected void page_load(object sender, eventargs e)     {         var cookiecount = request.cookies.count;     } } 

the web api , aspx pages in same project hosted in 1 site. not want use session variables , not want pass values in querystrings. there way of passing data routed page web api or missing here?

btw, if post api using postman, cookies visible in response header of web api, cookies created. if post using web page, using fiddler, can see cookies in response of api there no cookies in (receiving) asp page.

update answer of kai, can cookies in route asp page set in response.headers.location. have breakpoint in page know being hit , cookie count expected. however, browser not render routed page. remains on original posting page. here code i'm using in post emulator page call web api:

protected void page_load(object sender, eventargs e) {  }  protected async void doit_onclick(object sender, eventargs e) {     var reportrequest = new reportrequest();     reportrequest.emailaddress = email.text;     reportrequest.usernumber = usercode.text;     reportrequest.mobilenumber = mobilenumber.text;     reportrequest.password = password.text;     reportrequest.country = country.text;     reportrequest.accountnumber = accountnumber.text;     reportrequest.accounttype = accounttype.text;     reportrequest.reporttype = reporttype.text == "" ? 0 : convert.toint32(reporttype.text);     reportrequest.phoneinfo = phoneinfo.text;      await gothereasync(reportrequest);  }  public async task<uri> gothereasync(reportrequest reportrequest) {     using (var client = new httpclient())     {         client.baseaddress = new uri("http://localhost:7789/");         client.defaultrequestheaders.accept.clear();         client.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue("text/plain"));          var response = await client.postasjsonasync<reportrequest>("api/switch/reports", reportrequest);          if (response.issuccessstatuscode)         {             return response.requestmessage.requesturi;         }         return null;     } } 

to summarise: emulator.aspx post web api. web api sets cookies , location home.aspx. home.aspx receives cookies (debug steps code-behind) browser remains on emulat.aspx , not render home.aspx.

try cookieheadervalue.path = "/".

from microsoft docs:

path: restricts cookie specified path within domain. if not specified, path of request uri used.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -