wpf - WPF WebBrowser控件中的持久cookie?

标签 wpf cookies wpf-controls browser

我正在使用 WPF WebBrowser 在应用程序中显示在线帮助(只有几个小网页)。其中一些页面使用 cookie 仅在页面被查看的前几次显示项目(这是“为什么不尝试 X”类型的东西)。

但是,由于某种原因,cookie 似乎无法在 WebBrowser 控件中工作。它们在完整的 IE 以及 Firefox 和 Chrome 中运行良好(因此项目正确隐藏),但在通过 WPF WebBrowser 控件查看时它们从不隐藏。

在 WPF WebBrowser 控件中使用 cookie 有什么特别之处吗?它似乎表现得好像所有的 cookie 都只存储在内存中,而不是持久化在磁盘上。

这是浏览器中的其中一个页面(cookie 工作的地方):

Help pane inside a browser

这是应用程序内完全相同的页面:

Help pane inside the application

该附加内容应仅在使用该软件的最初几次可见(即它应该在该网页的 N 次浏览后隐藏),但因为我无法让 cookie 工作,它总是可见的。

最佳答案

Internet Explorer(或托管版本)中的 Cookie 处理与 IE 自己的“URL 安全区域”概念相关联,此处的文档:About URL security Zones

因此,IE 使用应用于 url 的各种算法来确定 url 区域。根据区域,您的托管浏览器可能支持也可能不支持 session 或持久性 cookie。

奇怪的是,当我创建一个小型 WPF 示例时,将 Web 浏览器添加到其中并导航到此持久性 cookie 测试器实用程序页面:http://www.rbaworld.com/Security/Computers/Cookies/givecook.shtml ,它工作正常。每次我启动示例应用程序时,计数器都会递增,因此不是每个人都能重现您的问题。嗯,这就是 URL 安全区域的全部目的:它可能因机器、用户、Windows 策略等而异......

下一个问题是:我可以更改您正在运行的区域吗?简短而简单的答案是……不,因为它与安全性密切相关。

如果您自己托管 IE,您可以实现自己的安全区域句柄,如下所述:Implementing a Custom Security Manager以及此处的示例:SAMPLE: Secumgr.exe Overrides Security Manager for WebBrowser Host但是您依赖于不允许任何覆盖的 WPF 网络浏览器...您可以访问 Reflector 并复制所有 WPF 私有(private)/内部代码,但这是有风险的工作日志!

您可以尝试的最后一件事是操纵标准 Internet 安全管理器。这是一些提供一些提示的示例代码。至少您应该能够确定您正在运行的区域 (MapUrltoZone) 并更改 cookie (TryAllowCookie)。大多数时候,标准管理器的问题是,它会向最终用户弹出允许授权的对话框......(再次安全!):

[ComImport, Guid("7b8a2d94-0ac9-11d1-896c-00c04Fb6bfc4")]
private class InternetSecurityManager
{
}

[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("79eac9ee-baf9-11ce-8c82-00aa004ba90b")]
private interface IInternetSecurityManager
{
    void Unused1();
    void Unused2();
    [PreserveSig]
    int MapUrlToZone([In, MarshalAs(UnmanagedType.BStr)] string pwszUrl, out int pdwZone, [In] int dwFlags);
    void Unused3();
    [PreserveSig]
    int ProcessUrlAction(string pwszUrl, int dwAction, ref int pPolicy, int cbPolicy, ref Guid pContext, int cbContext, int dwFlags, int dwReserved);
    // left undefined
}

public static SecurityZone MapUrlToZone(Uri uri)
{
    IInternetSecurityManager securityManager = (IInternetSecurityManager)new InternetSecurityManager();
    int zoneId;
    if (securityManager.MapUrlToZone(uri.ToString(), out zoneId, 0) < 0)
        return SecurityZone.NoZone;

    return (SecurityZone)zoneId;
}

private const int URLACTION_COOKIES = 0x00001A02;
private const int URLACTION_COOKIES_ENABLED = 0x00001A10;
private const int URLPOLICY_ALLOW = 0x00;
private const int URLPOLICY_DISALLOW = 0x03;
private const int PUAF_DEFAULT = 0x00000000;

public static bool TryAllowCookies(Uri uri)
{
    IInternetSecurityManager securityManager = (IInternetSecurityManager)new InternetSecurityManager();
    int policy = 0;
    Guid context = Guid.Empty;
    int hr = securityManager.ProcessUrlAction(uri.ToString(), URLACTION_COOKIES_ENABLED, ref policy, Marshal.SizeOf(policy), ref context, Marshal.SizeOf(context), PUAF_DEFAULT, 0);
    return (hr == 0) && policy == URLPOLICY_ALLOW;
}

祝你好运 :)

关于wpf - WPF WebBrowser控件中的持久cookie?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4278821/

相关文章:

WPF : Conditional templating of textblock

java - Android webview读取cookies

python - PDF 打印受密码保护的页面(最好让 wkhtmltopdf 使用 cookie)

c# - WPF:同步 ItemsControl 中所有项目的宽度

wpf - 如何在 XAML 中公开控件

wpf - 棱镜区域 UpdateRegionsException

c# - 如何创建可以处理 URL :callto and URL:tel protocols in Windows 10? 的 WPF 应用程序

javascript - 使用 cookie 代替字符串 - javascript

wpf - 如何在 wpf 中使用 MVVM 处理 ComboBox 的 SelectionChanged 事件?

c# - Caliburn 对模型-对象的微 Action