gpt4 book ai didi

c# - 如何清除特定站点的 WPF WebBrowser 中的 cookie?

转载 作者:太空狗 更新时间:2023-10-30 01:14:29 25 4
gpt4 key购买 nike

如何删除特定网站或其页面的身份验证 cookie?目前,如果我通过 WPF WebBrowser 使用 OAuth 2.0 登录,我的登录 session 将被保存,但我想在每次关闭我的应用程序时重置 session 。

public partial class VKLogin : Window
{
public string AccessToken { get; set; }

public VKLogin()
{
InitializeComponent();

this.Loaded += (object sender, RoutedEventArgs e) =>
{
webBrowser.Navigate("https://oauth.vk.com/authorize?client_id=5965945&scope=wall&redirect_uri=https://oauth.vk.com/blank.html&display=page&v=5.63&response_type=token");
};
}

private void webBrowser_Navigated(object sender, NavigationEventArgs e)
{
var url = e.Uri.Fragment;
if (url.Contains("access_token") && url.Contains("#"))
{
url = (new System.Text.RegularExpressions.Regex("#")).Replace(url, "?", 1);
AccessToken = System.Web.HttpUtility.ParseQueryString(url).Get("access_token");
}
}

private void Window_Closed(object sender, EventArgs e)
{
webBrowser.Dispose();
}
}

XAML

<Grid>
<WebBrowser Name="webBrowser"
Navigated="webBrowser_Navigated"
/>
</Grid>

最佳答案

如果你想删除所有的cookies,你可以使用InternetSetOption function , 解释 here .

代码如下:

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

private static unsafe void SuppressWininetBehavior()
{
/* SOURCE: http://msdn.microsoft.com/en-us/library/windows/desktop/aa385328%28v=vs.85%29.aspx
* INTERNET_OPTION_SUPPRESS_BEHAVIOR (81):
* A general purpose option that is used to suppress behaviors on a process-wide basis.
* The lpBuffer parameter of the function must be a pointer to a DWORD containing the specific behavior to suppress.
* This option cannot be queried with InternetQueryOption.
*
* INTERNET_SUPPRESS_COOKIE_PERSIST (3):
* Suppresses the persistence of cookies, even if the server has specified them as persistent.
* Version: Requires Internet Explorer 8.0 or later.
*/

int option = (int)3/* INTERNET_SUPPRESS_COOKIE_PERSIST*/;
int* optionPtr = &option;

bool success = InternetSetOption(0, 81/*INTERNET_OPTION_SUPPRESS_BEHAVIOR*/, new IntPtr(optionPtr), sizeof(int));
if (!success)
{
//Something went wrong
}
}

然后在 InitializeComponent(); 之后调用函数,例如:

SuppressWininetBehavior();

此外,如果您启用了 Javascript,则可以使用以下代码:

function delete_cookie( name ) {
document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}

关于c# - 如何清除特定站点的 WPF WebBrowser 中的 cookie?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43231921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com