When using CEFSharp in Visual Studio C# as an Embedded WebBrowser you need in accordance with user reqirements Disabling Web Security Check as a solution with this code:
var cefSettings = new CefSettings();
cefSettings.CefCommandLineArgs.Add("disable-web-security", "disable-web-security");
Cef.Initialize(cefSettings);
Microsoft WebView2 is a new product and I need now to perform same disabling of web security when using it in the same C# Coding Environment. I have searched a lot, but no possible solution.
Does anyone know how to this with WebView2 Settings?
Thanks in advance..
For WebView2 you can use CoreWebView2EnvironmentOptions.AdditionalBrowserArguments to set command line parameters for the browser process. These are the same command line parameters that the Edge browser accepts which mostly matches the chromium command line switches including --disable-web-security
.
If you are using the WPF or WinForms WebView2 control it would be something like the following:
CoreWebView2EnvironmentOptions options = new CoreWebView2EnvironmentOptions("--disable-web-security");
CoreWebView2Environment environment = await CoreWebView2Environment.CreateAsync(null, null, options);
// EnsureCoreWebView2Async must be called before any other call
// to EnsureCoreWebView2Async and before setting the Source property
// since these will both cause initialization of the CoreWebView2 property
// but using a default CoreWebView2Environment rather than your custom one.
await webview2.EnsureCoreWebView2Async(environment);
Note of course that you should not use disable web security without restricting the content rendered in the WebView2 to content you trust. Disabling web security and rendering unknown content in the WebView2 is dangerous.