void WebBrowserOnNavigating(object sender, NavigatingCancelEventArgs e)
{
// Check for a response to the callback url
const string portalApprovalMarker = "/oauth2/approval";
var webBrowser = sender as WebBrowser;
Uri uri = e.Uri;
// If no browser, uri, task completion source, or an empty url, return
if (webBrowser == null || uri == null || _tcs == null || string.IsNullOrEmpty(uri.AbsoluteUri))
return;
// Check for redirect
bool isRedirected = uri.AbsoluteUri.StartsWith(_callbackUrl) ||
_callbackUrl.Contains(portalApprovalMarker) && uri.AbsoluteUri.Contains(portalApprovalMarker);
if (isRedirected)
{
// If the web browser is redirected to the callbackUrl:
// -close the window
// -decode the parameters (returned as fragments or query)
// -return these parameters as result of the Task
e.Cancel = true;
var tcs = _tcs;
_tcs = null;
if (_window != null)
{
_window.Close();
}
// Call a helper function to decode the response parameters
var authResponse = DecodeParameters(uri);
// Set the result for the task completion source
tcs.SetResult(authResponse);
}
}