使用 C# 中的 System.EventArgs 属性。

huangapple go评论54阅读模式
英文:

Using the properties of a System.EventArgs in C#

问题

我有一个应用程序,之前使用System.Windows.Forms.WebBrowser来让用户导航到网页,以便进行OAuth2身份验证。这对于一个身份验证可以正常工作,但对于另一个身份验证,似乎WebBrowser不够现代化,所以我正在尝试用CefSharp的ChromiumWebBrowser替换WebBrowser。

这在某种程度上可以工作,我可以在一个名为e.Browser.FocusedFrame.Browser.Url的属性中看到我需要的字符串。请参见附带的调试模式下运行的应用程序的屏幕截图使用 C# 中的 System.EventArgs 属性。使用 C# 中的 System.EventArgs 属性。

我只需要能够解析我看到的字符串,查找"code=",然后提取在"code="和"&country="之间的字符串。在屏幕截图中,我需要"GB%2F68f9239a-80a5-4c66-bc2e-f40ec3fd35e9",这是授权码,用于交换令牌和刷新令牌。

我该如何访问这个属性?

提前感谢。
Jim.

我有一个名为"EventArgs e"的事件参数,似乎包含了我需要的包含代码的URL字符串,我希望能够做类似以下的事情:

string url = e.Browser.FocusedFrame.Browser.Url;

然后,我将能够获取参数列表,如果其中一个参数以"code="开头,我将获得用于交换的代码,工作就完成了。

之前的代码生成一个"WebBrowserNavigatedEventArgs e",应用程序可以解析"e.Url.Query"。请参见附带的屏幕截图EventArgs03.png使用 C# 中的 System.EventArgs 属性。

我只想做类似的事情。

谢谢,
Jim.

英文:

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使用 C# 中的 System.EventArgs 属性。使用 C# 中的 System.EventArgs 属性。 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.使用 C# 中的 System.EventArgs 属性。

I just want to do something similar.

Regards,
Jim.

答案1

得分: 0

标准和推荐的方法是使用匹配的方法签名来定义您的事件处理程序。

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;

// 定义一个事件处理程序方法,其签名与事件的委托签名匹配。
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"];
	}
}

可以在 https://github.com/cefsharp/CefSharp.MinimalExample/blob/cefsharp/113/CefSharp.MinimalExample.WinForms/BrowserForm.cs#L94-L100 找到一个可工作的示例。

英文:

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&lt;string, string&gt; parameters = null;
	var url = args.Browser.FocusedFrame.Url;
	if(url.Contains(&quot;code=&quot;))
	{
		int i = url.IndexOf(&#39;?&#39;);
		string query = url.Substring(i, url.Length - i);
		parameters = ParseFragment(query, new char[] { &#39;&amp;&#39;, &#39;?&#39; });
		this._authorizationCode = parameters[&quot;code&quot;];
	}
}

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

答案2

得分: -1

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.

英文:

使用 C# 中的 System.EventArgs 属性。The event I need to handle was ChroniumBrower.LoadingStateChanged :

this._chromiumBrowser.LoadingStateChanged += OnLoadingStateChanged;

Which fired :

    private void OnLoadingStateChanged(object sender, EventArgs e)
    {
        Dictionary&lt;string, string&gt; parameters = null;
        LoadingStateChangedEventArgs args =  LoadingStateChangedEventArgs)e;
        var url = args.Browser.FocusedFrame.Url;
        if(url.Contains(&quot;code=&quot;))
        {
            int i = url.IndexOf(&#39;?&#39;);
            string query = url.Substring(i, url.Length - i);
            parameters = ParseFragment(query, new char[] { &#39;&amp;&#39;, &#39;?&#39; });
            this._authorizationCode = parameters[&quot;code&quot;];
        }
        

    }

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.

huangapple
  • 本文由 发表于 2023年7月17日 18:11:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76703423.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定