//private const int INTERNET_OPTION_END_BROWSER_SESSION = 42; //[DllImport("wininet.dll", SetLastError = true)] //private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength); public MastodonOauth(string destinationUrl) { InitializeComponent(); this.Loaded += (object sender, RoutedEventArgs e) => { //Add the message hook in the code behind since I got a weird bug when trying to do it in the XAML //InternetSetOption(IntPtr.Zero, INTERNET_OPTION_END_BROWSER_SESSION, IntPtr.Zero, 0); WinInetHelper.SupressCookiePersist(); webBrowser.MessageHook += webBrowser_MessageHook; webBrowser.Navigate(destinationUrl); }; }
private void WebBrowser_OnLoadCompleted(object sender, NavigationEventArgs e) { var wb = (WebBrowser)sender; var codeElement = (wb.Document as dynamic).GetElementsByTagName("input")[0]; if (codeElement == null || codeElement.IHTMLElement_className != "oauth-code") { return; } var code = codeElement.IHTMLInputElement_defaultValue; if (string.IsNullOrWhiteSpace(code)) { return; } Code = code; WinInetHelper.EndBrowserSession(); Close(); }