Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I need to enable cookies in my WPF application WebBrowser control even if it is disabled in the IE settings. After going through many questions this is what i tried, but this does not work.

[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, ref int flag, int dwBufferLength);

    static bool EnableCookies(int settingCode, int option)
    {
        if (!InternetSetOption(IntPtr.Zero, settingCode, ref option, sizeof(int)))
        {
            var ex = Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error());
            //throw ex;
            return false;
        }
        return true;
    }

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        EnableCookies(81, 1);
    }

If this is not possible, I wish to at least be able to get the setting value to show the user an error message that cookies are not enabled.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
368 views
Welcome To Ask or Share your Answers For Others

1 Answer

webbrowser control uses wininet for networking, specifically use the internetSetCookie(Ex) and internetGetCookie(Ex) functions for Cookie management. There isn't a wininet wrapper in .Net, but you can p-invoke.

The WPF webbrowser does not expose all the features of the winforms webbrowser. The way to get around this is to host the winforms webbrowser in a WindowsFormsHost.

xmlns:my="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
xmlns:wb="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
Title="WpfWebBrowser" Height="300" Width="300">
<Grid>
    <my:WindowsFormsHost>
        <wb:WebBrowser x:Name="webBrowser"></wb:WebBrowser>
    </my:WindowsFormsHost>
</Grid>

see description here get-cookie-string-from-wpfs-webbrowser


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...