简体   繁体   English

在Windows Phone上保留HTTPOnly cookie

[英]Preserving HTTPOnly cookies on Windows Phone

I have an app that sends a username and password to an API via HTTPS. 我有一个应用程序通过HTTPS向API发送用户名和密码。 The API returns HTTPOnly cookies. API返回HTTPOnly cookie。

This means that the cookies are "invisible" to the code, but still exist and will be sent to the server in subsequent requests. 这意味着cookie对代码“不可见”,但仍然存在,并将在后续请求中发送到服务器。

The Set-Cookie header is stripped from the HttpWebResponse.Headers and the cookie does not appear in the HttpWebResponse.Cookie s or the HttpWebRequest.CookieContainer . HttpWebResponse.Headers剥离Set-Cookie标头,并且cookie不会出现在HttpWebResponse.CookieHttpWebRequest.CookieContainer However, if a subsequent request is made using that same HttpWebRequest.CookieContainer they are sent to the server, but they are inaccessible to the code. 但是,如果使用相同的HttpWebRequest.CookieContainer进行后续请求,则会将它们发送到服务器,但代码无法访问它们。

As far as I can tell, this makes them impossible to serialize or preserve in any way. 据我所知,这使得它们无法以任何方式序列化或保存。 It seems the only way to make this work will be to cache the actual username and password and login again every time. 看来,使这项工作的唯一方法是缓存实际的用户名和密码,每次都重新登录。

Is there something I am missing? 有什么我想念的吗?

You'll have to use reflection to take a look at the Cookies stored in the cookie container. 您必须使用反射来查看存储在cookie容器中的Cookie。

Use something like this to have a look at what you have, then you can either try to subclass to gain access to the data you want or go through the process of storing the cookie in memory, deleting it from the container, then adding it as a normal cookie 使用这样的东西看看你有什么,然后你可以尝试子类来获取你想要的数据,或者经历将cookie存储在内存中的过程,从容器中删除它,然后将其添加为一个普通的cookie

    public List<Cookie> GetAllCookies(CookieContainer cc)
    {
        List<Cookie> lstCookies = new List<Cookie>();

        Hashtable table = (Hashtable)cc.GetType().InvokeMember("m_domainTable", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.Instance, null, cc, new object[] { });

        foreach (var pathList in table.Values)
        {
            SortedList lstCookieCol = (SortedList)pathList.GetType().InvokeMember("m_list", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.Instance, null, pathList, new object[] { });
            foreach (CookieCollection colCookies in lstCookieCol.Values)
                foreach (Cookie c in colCookies) lstCookies.Add(c);
        }

        return lstCookies;
    }
    public string ShowAllCookies(CookieContainer cc)
    {
        StringBuilder sb = new StringBuilder();
        List<Cookie> lstCookies = GetAllCookies(cc);
        sb.AppendLine("=========================================================== ");
        sb.AppendLine(lstCookies.Count + " cookies found.");
        sb.AppendLine("=========================================================== ");
        int cpt = 1;
        foreach (Cookie c in lstCookies)
            sb.AppendLine("#" + cpt++ + "> Name: " + c.Name + "\tValue: " + c.Value + "\tDomain: " + c.Domain + "\tPath: " + c.Path + "\tExp: " + c.Expires.ToString());

        return sb.ToString();
    }

You can also try using TCP Sockets to get the cookies directly. 您也可以尝试使用TCP套接字直接获取cookie。 Here's my answer for a similar question: https://stackoverflow.com/a/21737087/262036 以下是我对类似问题的回答: https//stackoverflow.com/a/21737087/262036

Once you get the response you parse the string in search for the cookie and grab the value. 获得响应后,您将解析字符串以搜索cookie并获取值。 After that you can create a new cookie in the CookieContainer that is not HttpOnly and use it in next requests. 之后,您可以在CookieContainer中创建一个不是HttpOnly的新cookie,并在下一个请求中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM