WPF WebBrowser控件中的持久性Cookie? [英] Persistent cookies in WPF WebBrowser control?

查看:718
本文介绍了WPF WebBrowser控件中的持久性Cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用WPF WebBrowser在应用程序中显示在线帮助(只有几个小网页)。其中有些页面只在浏览页面的前几次使用Cookie来显示项目(这是一个为什么不尝试X类型的事情)。

I'm using the WPF WebBrowser to display online help inside an app (just a few small web pages). Some of those pages use cookies to display items only for the first few times the pages are viewed (it's a "Why not try X" type of thing).

但是,由于某种原因,Cookie似乎不能在WebBrowser控件中工作。它们在完整的IE以及Firefox和Chrome中工作正常(所以项目正确隐藏),但是当通过WPF WebBrowser控件查看时它们从不隐藏。

However, for some reason the cookies don't seem to be working inside the WebBrowser control. They work fine in full IE as well as Firefox and Chrome (so the items correctly hide), but they never hide when viewed through the WPF WebBrowser control.

特别关于在WPF WebBrowser控件中使用Cookie?看起来好像所有的cookie只存储在内存中,而不是保存在磁盘上。

Is there something special about using cookies in the WPF WebBrowser control? It seems to be behaving as if all the cookies are only stored in memory, rather than being persisted on disk.

这里是浏览器中的一个页面工作):

Here's one of those pages inside a browser (where the cookies work):

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

And here's the exact same page inside the app:

额外的内容应该只在前几次使用软件(即它应该在该网页的N个视图之后被隐藏),但是因为我不能得到cookie工作它总是可见的。

That additional content should only be visible for the first few times of using the software (i.e. it should be hidden after N views of that web page), but because I can't get cookies to work it's always visible.

推荐答案

Internet Explorer(或托管版本)中的Cookie处理与IE自己的URL安全区域概念有关,文档位于:关于网址安全区

因此,IE使用各种算法应用于url。根据区域,您的托管浏览器可能会或可能不支持会话或持久性Cookie。

So, IE determines an url zone using various alogorithms applied to the url. Depending on the zone, your hosted browser may or may not support session or persistent cookies.

奇怪的是,当我创建一个小WPF示例时,添加网络浏览器,请导航到此持久性Cookie测试人员网页: http://www.rbaworld.com/Security/Computers/ Cookies / givecook.shtml ,它工作正常。每次我启动示例应用程序,计数器增加罚款,所以不是每个人都可以重现您的问题。嗯,这是URL安全区域的全部目的:它可以根据机器,用户,Windows策略等等...

Strangely, when I create a small WPF sample, add the web browser to it and have navigate to this persistent cookie tester utiliy page: http://www.rbaworld.com/Security/Computers/Cookies/givecook.shtml, it works fine. Each time I launch the sample app, the counter is incremented fine, so not everyone can reproduce your problem. Well, that's the whole purpose of URL Security zones: it can vary by machine, by user, by Windows policy, etc...

下一个问题是:我可以更改您运行的区域?

The next question is: Can I change the zone you're running in? The short and easy answer is ... no because it's heavily tied to the security.

如果你自己托管IE,你可以实现你自己的安全区域句柄这里:实现自定义安全管理器和此处的示例: SAMPLE:Secumgr.exe覆盖WebBrowser主机的安全管理器,但你依靠WPF的webbrowser不允许任何覆盖。 ..你可以得到反思,并复制所有WPF私人/内部代码,但这是一个危险的工作的日志!

If you were hosting IE yourself, you could implement your own security zone handle as described here: Implementing a Custom Security Manager and a sample here: SAMPLE: Secumgr.exe Overrides Security Manager for WebBrowser Host but you're relying on WPF's webbrowser that does not allow any override... You can get to Reflector and copy all WPF private/internal code but that's a log of risky work!

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

The last thing you can try is to manipulate the standard Internet Security Manager. Here is some sample code that gives some hints. At least you should be able to determine the zone you're running in (MapUrltoZone) and change the cookie (TryAllowCookie). The problem with the standard manager is most of the times, it pops up dialog to the end-user allowing authorization... (security again!):

[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 WebBrowser控件中的持久性Cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆