4

update1:​​さらに調査した結果、これが可能かどうかはわかりませんが、修正時にUserVoiceエントリを作成しました

アプリの終了時またはトゥームストーニングが発生したときにCookieContainerを保存しようとしていますが、いくつかの問題が発生しました。

AppSettingsにCookieContainerを保存しようとしましたが、読み込まれるとCookieが失われます。

Researching this internally, DataContractSerializer cannot serialize cookies.
This seems to be a behavior that Windows Phone inherited from Silverlight's DataContractSerializer.

さらに調査を行った後、回避策はコンテナからCookieを取得し、別の方法で保存することであるように思われました。私が別の障害にぶつかるまで、それはうまくいきました。.mydomain.comのURIでGetCookiesを取得できません。私はそれがこのバグのせいであると信じています。ドメインテーブルにCookie.mydomain.comが表示されますが、GetCookiesはその特定のCookieでは機能しません。

バグはここに再び投稿されます

ドメインが。:で始まる場合、コンテナからCookieを取得することにも問題があります。

CookieContainer container = new CookieContainer();
container.Add(new Cookie("x", "1", "/", ".blah.com"));
CookieCollection cv = container.GetCookies(new Uri("http://blah.com"));
cv = container.GetCookies(new Uri("http://w.blah.com"));

リフレクションを使用してドメインテーブルを反復処理し、「。」を削除することで回避策を見つけました。プレフィックス。

private void BugFix_CookieDomain(CookieContainer cookieContainer)
{
    System.Type _ContainerType = typeof(CookieContainer);
    var = _ContainerType.InvokeMember("m_domainTable",
                               System.Reflection.BindingFlags.NonPublic |
                               System.Reflection.BindingFlags.GetField |
                               System.Reflection.BindingFlags.Instance,
                               null,
                               cookieContainer,
                               new object[] { });
    ArrayList keys = new ArrayList(table.Keys);
    foreach (string keyObj in keys)
    {
        string key = (keyObj as string);
        if (key[0] == '.')
        {
            string newKey = key.Remove(0, 1);
            table[newKey] = table[keyObj];
        }
    }
}

InvokeMemberが呼び出された場合のみ、SLでMethodAccessExceptionがスローされます。保存する必要のあるCookieの1つがHttpOnlyであるため、これは実際には私の問題を解決しません。これがCookieContainerの理由の1つです。

サーバーがHTTPOnlyCookieを送信する場合は、Cookieを保持する要求に対してSystem.Net.CookieContainerを作成する必要がありますが、コンテナーに格納されているCookieは表示されないか、アクセスできません。

それで、何かアイデアはありますか?私は何か簡単なものが欠けていますか?CookieContainerの状態を保存する別の方法はありますか、それともパスワードを含むユーザー情報を保存して、アプリが起動するたびに、そして墓石から戻ったときにユーザーを再認証する必要がありますか?

4

2 に答える 2

2

この問題に具体的に対処する CookieSerializer を作成しました。シリアライザを以下に貼り付けます。実際のプロジェクトとシナリオについては、プロジェクトのCodePlex サイトにアクセスしてください。

public static class CookieSerializer
{
    /// <summary>
    /// Serializes the cookie collection to the stream.
    /// </summary>
    /// <param name="cookies">You can obtain the collection through your <see cref="CookieAwareWebClient">WebClient</see>'s <code>CookieContainer.GetCookies(Uri)</code>-method.</param>
    /// <param name="address">The <see cref="Uri">Uri</see> that produced the cookies</param>
    /// <param name="stream">The stream to which to serialize</param>
    public static void Serialize(CookieCollection cookies, Uri address, Stream stream)
    {
        using (var writer = new StreamWriter(stream))
        {
            for (var enumerator = cookies.GetEnumerator(); enumerator.MoveNext();)
            {
                var cookie = enumerator.Current as Cookie;
                if (cookie == null) continue;
                writer.WriteLine(address.AbsoluteUri);
                writer.WriteLine(cookie.Comment);
                writer.WriteLine(cookie.CommentUri == null ? null : cookie.CommentUri.AbsoluteUri);
                writer.WriteLine(cookie.Discard);
                writer.WriteLine(cookie.Domain);
                writer.WriteLine(cookie.Expired);
                writer.WriteLine(cookie.Expires);
                writer.WriteLine(cookie.HttpOnly);
                writer.WriteLine(cookie.Name);
                writer.WriteLine(cookie.Path);
                writer.WriteLine(cookie.Port);
                writer.WriteLine(cookie.Secure);
                writer.WriteLine(cookie.Value);
                writer.WriteLine(cookie.Version);
            }
        }
    }

    /// <summary>
    /// Deserializes <see cref="Cookie">Cookie</see>s from the <see cref="Stream">Stream</see>, 
    /// filling the <see cref="CookieContainer">CookieContainer</see>.
    /// </summary>
    /// <param name="stream">Stream to read</param>
    /// <param name="container">Container to fill</param>
    public static void Deserialize(Stream stream, CookieContainer container)
    {
        using (var reader = new StreamReader(stream))
        {
            while (!reader.EndOfStream)
            {
                var uri = Read(reader, absoluteUri => new Uri(absoluteUri, UriKind.Absolute));
                var cookie = new Cookie();
                cookie.Comment = Read(reader, comment => comment);
                cookie.CommentUri = Read(reader, absoluteUri => new Uri(absoluteUri, UriKind.Absolute));
                cookie.Discard = Read(reader, bool.Parse);
                cookie.Domain = Read(reader, domain => domain);
                cookie.Expired = Read(reader, bool.Parse);
                cookie.Expires = Read(reader, DateTime.Parse);
                cookie.HttpOnly = Read(reader, bool.Parse);
                cookie.Name = Read(reader, name => name);
                cookie.Path = Read(reader, path => path);
                cookie.Port = Read(reader, port => port);
                cookie.Secure = Read(reader, bool.Parse);
                cookie.Value = Read(reader, value => value);
                cookie.Version = Read(reader, int.Parse);
                container.Add(uri, cookie);
            }
        }
    }

    /// <summary>
    /// Reads a value (line) from the serialized file, translating the string value into a specific type
    /// </summary>
    /// <typeparam name="T">Target type</typeparam>
    /// <param name="reader">Input stream</param>
    /// <param name="translator">Translation function - translate the read value into 
    /// <typeparamref name="T"/> if the read value is not <code>null</code>.
    /// <remarks>If the target type is <see cref="Uri">Uri</see> , the value is considered <code>null</code> if it's an empty string.</remarks> </param>
    /// <param name="defaultValue">The default value to return if the read value is <code>null</code>.
    /// <remarks>The translation function will not be called for null values.</remarks></param>
    /// <returns></returns>
    private static T Read<T>(TextReader reader, Func<string, T> translator, T defaultValue = default(T))
    {
        var value = reader.ReadLine();
        if (value == null)
            return defaultValue;
        if (typeof(T) == typeof(Uri) && String.IsNullOrEmpty(value))
            return defaultValue;
        return translator(value);
    }
}
于 2011-12-09T17:30:34.807 に答える
0

リフレクションを使用しても、WP7 のアセンブリの外部にあるプライベート メンバーにアクセスすることはできません。これは、内部システム API を呼び出せないようにするためのセキュリティ対策です。

運が悪い可能性があるようです。

于 2011-09-27T08:32:08.713 に答える