在Windows Phone上保留HTTPOnly cookie

zi3*_*guw 11 .net c# http windows-phone windows-phone-8

我有一个应用程序通过HTTPS向API发送用户名和密码.API返回HTTPOnly cookie.

这意味着cookie对代码"不可见",但仍然存在,并将在后续请求中发送到服务器.

Set-Cookie报头被从汽提HttpWebResponse.Headers和cookie不出现在HttpWebResponse.CookieS或的HttpWebRequest.CookieContainer.但是,如果使用相同的请求进行后续请求,则将HttpWebRequest.CookieContainer它们发送到服务器,但代码无法访问它们.

据我所知,这使得它们无法以任何方式序列化或保存.看来,使这项工作的唯一方法是缓存实际的用户名和密码,每次都重新登录.

有什么我想念的吗?

Ste*_*tad 3

您必须使用反射来查看存储在 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();
    }
Run Code Online (Sandbox Code Playgroud)