The task of the program is such that when the application is opened, an automatic login to the site occurs, which requests HTTP authentication, provided that the login, password and URL are entered in advance.
I tried to embed login and password in the address bar using this method:
if (auth.URL.Contains(@"http://") || auth.URL.Contains(@"http:\\"))
{
auth.URL = $"http://{auth.Login}:{auth.Password}@{auth.URL.Remove(0, 7)}/";
}
else if (auth.URL.Contains(@"https://") || auth.URL.Contains(@"https:\\"))
{
auth.URL = $"https://{auth.Login}:{auth.Password}@{auth.URL.Remove(0, 8)}/";
}
else
{
auth.URL = $"http://{auth.Login}:{auth.Password}@{auth.URL}/";
}
But I noticed that if I paste the test login "ENTERPRISE\A.Example" and the password "#Mdm256$" into the address bar, the site cannot open normally. I figured out it was because of the \ and # signs. I also tried to write a custom MyRequestHandler
class with the GetAuthCredentials()
method:
protected override bool GetAuthCredentials(IWebBrowser chromiumWebBrowser, IBrowser browser, string originUrl, bool isProxy, string host, int port, string realm, string scheme, IAuthCallback callback)
{
callback.Continue(username, password);
return true;
}
But it is not called or used in the code and there was no information anywhere on how to call it
You need to assign your custom RequestHandler
instance to the ChromiumWebBrowser.RequestHandler
property as shown below:
browser.RequestHandler = new CustomRequestHandler();
public class CustomRequestHandler : CefSharp.Handler.RequestHandler
{
protected override bool GetAuthCredentials(IWebBrowser chromiumWebBrowser, IBrowser browser, string originUrl, bool isProxy, string host, int port, string realm, string scheme, IAuthCallback callback)
{
//Cast IWebBrowser to ChormiumWebBrowser if you need to access the UI control
//You can Invoke onto the UI Thread if you need to show a dialog
var b = (ChromiumWebBrowser)chromiumWebBrowser;
if(!isProxy)
{
using (callback)
{
callback.Continue(username: "user", password: "pass");
}
return true;
}
//Return false to cancel the request
return false;
}
}