JBildstein / SpiderEye

Cross platform .Net Core applications with a webview UI
Apache License 2.0
217 stars 22 forks source link

Access Denied #26

Open daniel-scatigno opened 3 years ago

daniel-scatigno commented 3 years ago

I'm running on Windows 10 19042.630 I have installed the template, when running the application targeting net5.0-windows (With Edge Canary Installed) it throws a "CLR/System.UnauthorizedAccessException"

Here is the entire exception

Exception has occurred: CLR/System.UnauthorizedAccessException An unhandled exception of type 'System.UnauthorizedAccessException' occurred in System.Private.CoreLib.dll: 'Access is denied. (0x80070005 (E_ACCESSDENIED))' at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode) at Microsoft.Web.WebView2.Core.CoreWebView2Environment.d3.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at SpiderEye.Windows.EdgiumWebview.d28.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at SpiderEye.Windows.WinFormsTaskExtensions.RunSyncWithPump(Task task) at SpiderEye.Windows.EdgiumWebview..ctor(String hostAddress, WebviewBridge bridge) at SpiderEye.Windows.WinFormsWindow..ctor(WebviewBridge bridge) at SpiderEye.Windows.WinFormsUiFactory.CreateWindow(WindowConfiguration config, WebviewBridge bridge) at SpiderEye.Window..ctor() at testwebview.Core.ProgramBase.Run() in D:\Temp\testwebview\testwebview.Core\ProgramBase.cs:line 9 at testwebview.Program.Main(String[] args) in D:\Temp\testwebview\testwebview.Windows\Program.cs:line 13

By the way, the launch.json from the template is outdated, it points out to .net 3.0 framework instead 5.0

This project looks very promising. I'll test on linux later! Great Work!

JBildstein commented 3 years ago

Thank you for the report. It looks like this is the issue https://github.com/MicrosoftEdge/WebView2Feedback/issues/297 or https://github.com/MicrosoftEdge/WebView2Feedback/issues/271 In the second issue it says

it will try to create the cache directory in the directory of the executable

Not sure if your app or Edge is meant by executable but a temporary workaround may be to change the folder permissions. Or use the WebView2 runtime instead of Edge Canary, which seems to work.

To actually fix this, I'll have to manually set the cache folder path which should be pretty easy.

Thanks for the notice on launch.json, I had already fixed it but forgot to publish the Templates package.

bddckr commented 2 years ago

I'm running into this issue. My customers are getting a chance to install my app into a per-user folder or to a per-machine location. The latter means that the installation folder of the app is read-only, as we don't want a specific user's data to be usable by another user. We store all our data in user-specific folders instead, but WebView2's folder is indeed created next to the executable.

More info can be found in the WebView2 documentation nowadays: https://docs.microsoft.com/en-us/microsoft-edge/webview2/concepts/user-data-folder?tabs=dotnet


I'm interested in resolving this and opening a PR to do so. Does the following sound correct, @JBildstein?

  1. Add a new static property to WindowsApplication:
    public static Action<Task<CoreWebView2Environment>> CreateWebView2Environment { get; set; } = CoreWebView2Environment.CreateAsync;
  2. Adjust EdgiumWebview to call that property.

I suggest doing it this way because CoreWebView2Environment.CreateAsync takes a bunch of optional parameters, and I don't think it's worth creating a POCO just for that purpose. The SpiderEye user will have a chance to set the property to something besides the default value, allowing them to overcome this issue and to set various other settings.

Alternatively, we could make it an (optional?) argument to the Init method, but I'm not sure if you require binary compatibility. With the suggested property, we also follow how the existing WebviewType property works already (though it looks like a user can't actually set that since Init just resets it back to Latest - something for another issue).

bddckr commented 2 years ago

Oh!

Turns out WebView2 supports looking up environment variables for this. I've successfully used the following:

Environment.SetEnvironmentVariable("WEBVIEW2_USER_DATA_FOLDER", Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "MyCompany", "MyProduct", "Caches", "WebView"))

I'll retract my offer for a PR because of this 😄