Jim Borland
Jim Borland

Reputation: 47

Using the properties of a System.EventArgs in C#

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 shotsApp in debug mode.App in debug mode. 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.Old WebBrowser with parseable url

I just want to do something similar.

Regards, Jim.

Upvotes: 1

Views: 380

Answers (2)

amaitland
amaitland

Reputation: 4410

The standard and recommended approach is to define your event handler with a matching method signature.

https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/events/how-to-subscribe-to-and-unsubscribe-from-events#to-subscribe-to-events-programmatically

_chromiumBrowser.LoadingStateChanged += OnLoadingStateChanged;

// Define an event handler method whose signature matches the delegate signature for the event.
private void OnLoadingStateChanged(object sender, LoadingStateChangedEventArgs args)
{
    Dictionary<string, string> parameters = null;
    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"];
    }
}

There is a working example available at https://github.com/cefsharp/CefSharp.MinimalExample/blob/cefsharp/113/CefSharp.MinimalExample.WinForms/BrowserForm.cs#L94-L100

Upvotes: 0

Jim Borland
Jim Borland

Reputation: 47

LoadingStateChangedEventArgsThe 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.

Upvotes: -1

Related Questions