在WPF WebBrowser控件永久Cookie? [英] Persistent cookies in WPF WebBrowser control?

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

问题描述

我使用WPF WebBrowser控件显示一个应用程序(只是几个小网页)内的联机帮助。一些页面使用cookies只为第几次浏览页面(这是一个为什么不试试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).

然而,由于某种原因,饼干似乎并不在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控件使用cookies吗?这似乎是表现得好像所有的饼干只存储在内存中,而不是坚持在磁盘上。

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.

下面是一个浏览器中这些网页(其中Cookie的工作)之一:

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

和这里的内部应用程序完全相同的页面:

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

这是额外的内容应该只使用软件的最初几次是可见的(即它应该N个视点的网页后隐藏的),而是因为我不能让cookies来工作,它总是可见的。

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处理(或托管版本)是联系在一起的IE自身的URL安全区域的概念,DOC:的关于URL安全区域

Cookies handling in Internet Explorer (or hosted versions) is tied to the IE's own notion of "URL Security Zones", doc here: About URL security Zones

所以,IE使用决定适用于各种URL的alogorithms 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,Web浏览器添加到它并导航到这个永久性的Cookie测试utiliy页:<一href=\"http://www.rbaworld.com/Security/Computers/Cookies/givecook.shtml\">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浏览器,你可以按此处所述实现自己的安全区手柄:的在这里实现自定义安全管理器和示例:示例:Secumgr.exe覆盖安全管理器的web浏览器主持人但你依赖于WPF的网页浏览器,不允许任何超越...你可以得到反射并复制私有WPF /内部code,但是这是一个记录的危险工作!

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!

您可以尝试的最后一件事是操作标准的Internet安全管理。下面是一些示例code,它提供了一些线索。至少你应该能够决定你的(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天全站免登陆