存储在asmx Web服务的响应标头中收到的cookie

时间:2019-04-11 19:03:34

标签: cookies http-headers asmx http-get

我正在尝试通过令牌标头向https://www.test.com认证API中的用户

我收到带有标题键的响应:

"Set-Cookie="csrftoken=hOmAjkdBzlaYtNLN4w0YivRjPfZT0xpfl1I3cA933YNE0mZfflRV7NwzdrEjFfni; expires=Thu, 09-Apr-2020 18:54:25 GMT; Max-Age=31449600; Path=/,sessionid=kvplyt7poch4n34hqjvasohzqhgrtv0u; expires=Thu, 25-Apr-2019 18:54:25 GMT; HttpOnly; Max-Age=1209600; Path=/"

我正尝试在浏览器中为网站https://www.test.com创建一个新的cookie,其中包含接收到的内容(csrftoken,过期,域等)。

这样,当我打开一个新窗口时,该cookie将已经存在并且将对用户进行身份验证。

[WebMethod]
public string authenticateUser(string token) {
  //Create a request for the URL.
  var request = WebRequest.Create("https://www.test.com");
  var myHttpWebRequest = (HttpWebRequest)request;
  string response1 = "";
  myHttpWebRequest.ContentType = "text/html";
  myHttpWebRequest.Method = "GET";
  myHttpWebRequest.Headers.Add("token", token);
  try {
    using (WebResponse resp = myHttpWebRequest.GetResponse()) {
      if (resp == null)
        new Exception("Response is null");

      System.IO.Stream test2 = resp.GetResponseStream();
      StreamReader reader = new StreamReader(resp.GetResponseStream());
      string test1 = reader.ToString();
      response1 = reader.ReadToEnd();

      //Some parsing to retrieve the header value I want to set

      //Actual cookie generation
      HttpCookie csrftoken = new HttpCookie("csrftoken", "MzsasdsIb2DtR1h3Tzwje6Mgb0cdsdVjDCYZAsdsdRBmsds6ds7ubZgdsds4r6Jl99hysddssbSNG3");
      csrftoken.Expires = DateTime.Now.AddYears(1);
      csrftoken.Path = "/";
      csrftoken.Domain = ".test";
      HttpContext.Current.Response.Cookies.Add(csrftoken);
    }
  }
  catch (Exception ex) {
    return ex.Message;
  }
  return "OK";
 }

不幸的是,当我完成所有这些操作后,cookie会存储在localhost而不是目标API的URI。

无论如何,对于IE 11,使用纯asmx.cs网络服务和/或香草javascript是否可以实现这一目标?

0 个答案:

没有答案