microsoft-ui-xaml
                                
                                
                                
                                    microsoft-ui-xaml copied to clipboard
                            
                            
                            
                        Question: WebView2 requires elevated mode ?
Attempted to create and initialize WebView2 but Access Denied. This code is supposed to work only in elevated mode or a particular Windows policy required?

0x80070005 Indicates incorrect permissions; Not necessarily that you need to be elevated.
Are you running your program as a limited user?
Does your user or group have permissions on the ACL for the folder (Permissions)?
edit:
krschau said:
About the original issue: the Access Denied error is expected when you use a WebView2 inside a WinUI 3 unpackaged app. This is because the default location of the User Data Folder ends up being in a protected directory
Logged in as local administrator to windows but VS wasn't running as administrator.
EnsureCoreWebView2Async seems to throw 0x80070005 when it can't create a user data folder. Do you specify the path of the user data folder?
Reference: https://github.com/MicrosoftEdge/WebView2Feedback/issues/1128
How to specify UDF ? That example is missing on GitHub.

Access Denied even already at new CoreWebView2EnvironmentOptions();
I assume something like that shall be carried out for unpackaged products :

In the WebView2 Win32 C ++ API, you can specify the path of the user data folder with the environment variable "WEBVIEW2_USER_DATA_FOLDER".
My app written in C++/WinRT and WinUI3 was able to specify a user data folder by calling the SetEnvironmentVariable function.
SetEnvironmentVariable(L"WEBVIEW2_USER_DATA_FOLDER", [Path to user data folder]);
However, as I reported in #7201, my app worked fine on Windows App SDK 1.0.3, but no longer works on machines with Windows App SDK 1.1.0 installed.
Cool, I am glad you succeeded to run the control . Yeah it smells a bit since in line 49 just attempted to invoke a default ctor. This param isn't necessary in your case.
@sigmarsson Creating custom environment options is not currently supported in WinUI. When you do need to specify a custom User Data Foder, @osamu-ikawa's suggestion of using the environment variable would be the way to do so at this point. Doing it directly via environment options is something we want to support in the future.
About the original issue: the Access Denied error is expected when you use a WebView2 inside a WinUI 3 unpackaged app. This is because the default location of the User Data Folder ends up being in a protected directory, and we can't create the folder there. Generally you'll want to handle the user data folder location and cleanup for this type of app. More details and guidance can be found in the Manage user data folders doc. Looks like we need to add a section to the "WinUI 3" tabs to give guidance for unpackaged apps -- I think it would look a lot like what's currently in the "Win32" tabs. I opened an issue to update the doc.
@krschau As mentioned in the comments above (https://github.com/microsoft/microsoft-ui-xaml/issues/7190#issuecomment-1150660241), my WinUI3 unpackaged app used environment variables to set a custom user data folder and it worked fine with Windows App SDK 1.0.3.
However, after installing Windows App SDK 1.1.0, it stopped working properly. I thought this was a bug in the Windows App SDK 1.1.0 and reported it in #7201, but the issue has already been closed.
I expect the custom data folder settings using environment variables to work even after installing Windows App SDK 1.1.X.
I confirm. 1.1.0 not working even having the variable set.

I confirm. 1.1.0 not working even having the variable set.
I get Access Denied on :
LoadLibraryExW( "C:\Program Files\WindowsApps\Microsoft.WindowsAppRuntime.1.1_1000.516.2156.0_x86__8wekyb3d8bbwe\Microsoft.Web.WebView2.Core.dll", NULL, LOAD_WITH_ALTERED_SEARCH_PATH )  
So, if I copy Microsoft.Web.WebView2.Core.dll in my app directory, it works. But this bug should be fixed asap...
@krschau "Looks like we need to add a section to the "WinUI 3" tabs to give guidance for unpackaged apps"
Probably "unpackaged" scenario shall be the default one everywhere, everytime, since we all package after the product is done or at least RC. 1.1.2 ?
@krschau Which version can we expect the hotfix will ship and when ?
I can't reproduce the error -- I can use the environment variable to set a custom user data folder with no elevation using WinAppSDK 1.1.0. I too am logged in as admin, but not running VS in admin mode, although I agree with @HypsyNZ that it's probably a permissions thing, not elevation.
@sigmarsson I am seeing issues when I wait for EnsureCoreWebView2Async() as you did above, with t.AsTask().Wait(). Could you try just await this.WebView.EnsureCoreWebView2Async();? I wonder if the exception is really coming from the waiting.
Similarly, @castorix, could there be an issue with LoadLibraryExW? Are you saying the exception hit when you added that, or it was part of the call stack when you were debugging WebView2 creation?
Similarly, @castorix, could there be an issue with LoadLibraryExW? Are you saying the exception hit when you added that, or it was part of the call stack when you were debugging WebView2 creation?
It is the error I saw by debugging : it could not load Microsoft.Web.WebView2.Core.dll with Access Denied error While I have all rights to C:\Program Files\WindowsApps..., as I could copy Microsoft.Web.WebView2.Core.dll from there into the .exe directory and then it worked

I confirm. 1.1.0 not working even having the variable set.
I get Access Denied on :
LoadLibraryExW( "C:\Program Files\WindowsApps\Microsoft.WindowsAppRuntime.1.1_1000.516.2156.0_x86__8wekyb3d8bbwe\Microsoft.Web.WebView2.Core.dll", NULL, LOAD_WITH_ALTERED_SEARCH_PATH )So, if I copy Microsoft.Web.WebView2.Core.dll in my app directory, it works. But this bug should be fixed asap...
Has the ACL (Permissions) for the folder Microsoft.WindowsAppRuntime.1.1_1000.516.2156.0_x86__8wekyb3d8bbwe changed after updating? or the current user doesn't have access to the WindowsApps folder?
Since the path obviously exists it is probably a permissions problem.

Any progress on this issue?
In my unpackaged app, I get the 0x80070005 (Access denied) error when calling cookie manager API like below.
CoreWebView2().CookieManager().DeleteAllCookies();
CoreWebView2().CookieManager().AddOrUpdateCookie(cookie);
The 0x80070005 error happens with following backtrace.
D:\a\_work\1\s\dev\DynamicDependency\API\MddWinRT.cpp(54)\Microsoft.WindowsAppRuntime.dll!00007FFA80C4C592: (caller: 00007FFA80C2DCD3) ReturnHr(10) tid(5a0c) 80070005 Access denied.
    Msg:[D:\a\_work\1\s\dev\DynamicDependency\API\WinRTInprocModule.h(106)\Microsoft.WindowsAppRuntime.dll!00007FFA80C2771A: (caller: 00007FFA80C27857) Exception(3) tid(5a0c) 80070005 Access denied.
] 
D:\a\_work\1\s\dev\UndockedRegFreeWinRT\catalog.cpp(347)\Microsoft.WindowsAppRuntime.dll!00007FFA80C2DCF5: (caller: 00007FFA52A92817) ReturnHr(11) tid(5a0c) 80070005 Access denied.
D:\a\_work\1\s\dev\UndockedRegFreeWinRT\catalog.cpp(333)\Microsoft.WindowsAppRuntime.dll!00007FFA80C2DD35: (caller: 00007FFA52A92817) ReturnHr(12) tid(5a0c) 80070005 Access denied.
D:\a\_work\1\s\dev\UndockedRegFreeWinRT\urfw.cpp(246)\Microsoft.WindowsAppRuntime.dll!00007FFA80C2DD51: (caller: 00007FFA52A92817) ReturnHr(13) tid(5a0c) 80070005 Access denied.
The issue happens only when I use WinAppSDK 1.1.x and doesn't happen when I use WinAppSDK 1.0.x. So I think this is caused by same UDF permission related issue of WinAppSDK 1.1.
I just tried to repro this in an unpackaged WinUI 3 app using the latest WinAppSDK 1.2.221109.1 and setting the WEBVIEW2_USER_DATA_FOLDER environment variable, and I think I'm still able to repro this bug. In my case it renders a blank page. Here's a repo where it repros: https://github.com/lyahdav/MyWebView2WinUI3-2.
Package jor app with the template studio to get all this immediate running. Unpackaged application supported but with a pinch of salt merely. Not worth spending the time .
This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 5 days.
This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 5 days.
Per my previous comment this still seems to be an issue even when setting the WEBVIEW2_USER_DATA_FOLDER environment variable.