I have an application which was using a System.Windows.Forms.WebBrowser to enable users to navigate to a web page in order to allow an OAuth2 autentication to take place. This works for one authentication but for another it seems that WebBrowser isn't modern enough, so I'm attempting to replace WebBrowser with a ChromiumWebBrowser from CefSharp.
This is more or less working in that I can see the string I require in a property called :
e.Browser.FocusedFrame.Browser.Url
See the attached screen shots of my app running in debug mode.
I just need to be able to parse the string that I can see is there looking for "code=" then extract the string between "code=" and "&country=". In the screen shot I need the "GB%2F68f9239a-80a5-4c66-bc2e-f40ec3fd35e9", which is the authorisation code, to exchange for a token and refresh token.
How do I get access this property?
Thanks in advance. Jim.
I've got an "EventArgs e" which appears to contain the url string containing the code I require and I'm hoping to be able to say something like :
string url = e.Browser.FocusedFrame.Browser.Url;
Then I'll be able get a list of the the parameters and if one of the parameters begins with "code=" I'll have my code for the exchange and that's job done.
The previous code produces a "WebBrowserNavigatedEventArgs e" and the app is able to parse "e.Url.Query". See the attached screen shot EventArgs03.png.
I just want to do something similar.
Regards, Jim.
The event I need to handle was ChroniumBrower.LoadingStateChanged
:
this._chromiumBrowser.LoadingStateChanged += OnLoadingStateChanged;
Which fired :
private void OnLoadingStateChanged(object sender, EventArgs e)
{
Dictionary<string, string> parameters = null;
LoadingStateChangedEventArgs args = LoadingStateChangedEventArgs)e;
var url = args.Browser.FocusedFrame.Url;
if(url.Contains("code="))
{
int i = url.IndexOf('?');
string query = url.Substring(i, url.Length - i);
parameters = ParseFragment(query, new char[] { '&', '?' });
this._authorizationCode = parameters["code"];
}
}
I was struggling to gain access to the required property of e but by adding the line :
LoadingStateChangedEventArgs args = (LoadingStateChangedEventArgs)e;
I was able to parse args.Browser.FocusedFrame.Url
which is where the string I need was stored.