Azure-Samples / active-directory-b2c-dotnet-desktop

Sample showing how a Windows desktop .NET (WPF) application can sign in a user using Azure AD B2C, get an access token using MSAL.NET and call an API.
http://aka.ms/aadb2c
MIT License
78 stars 38 forks source link

Error Acquiring Token - Access to token cache denied #71

Closed leskodan closed 3 years ago

leskodan commented 3 years ago

Hi there! This is a great sample that was straight forward to connect to my own B2C tenant and get working quickly.

As I start to experiment with it however, I am running into some issues. I would like to be able to package and distribute an application protected against B2C as a .msix. Having done so, I am running into an issue where an installed copy of the sample is not permitted access to the token cache.


System.UnauthorizedAccessException: Access to the path 'C:\Program Files\WindowsApps\wpfInstallerTest_1.0.3.0_x64__3ae7ag8q269pa\active-directory-b2c-wpf\active_directory_b2c_wpf.exe.msalcache.bin' is denied.```

Is there something that I need to do to make sure that the app can get access to the token cache of my distributed users? It is currently preventing a successful sign in.
jmprieur commented 3 years ago

@leskodan you might want to borrow the following implementation of this token cache: https://github.com/Azure-Samples/active-directory-dotnet-desktop-msgraph-v2/blob/msal3x/active-directory-wpf-msgraph-v2/TokenCacheHelper.cs, which, I believe I've modified for this issue

@Shama-K : do you mind please updating the token cache helper of this sample with that cache (of the AAD sample)?

leskodan commented 3 years ago

Appreciate it @jmprieur! I'll check it out and let you know if I have issues.

leskodan commented 3 years ago

image

@jmprieur This block definitely clarifies for me what is going on here. That said, Windows is not defined here. What package does this depend on?

jmprieur commented 3 years ago

@leskodan, I think Microsoft.Windows.SDK.Contracts. See https://github.com/Azure-Samples/active-directory-dotnet-desktop-msgraph-v2/blob/da9b6673b0cefef430f5228ba985490441e0a8e6/active-directory-wpf-msgraph-v2/active-directory-wpf-msgraph-v2.csproj#L109-L111

leskodan commented 3 years ago

@jmprieur appreciate the help. Unfortunately I'm still having issues though. Having installed the Microsoft.Windows.SDK.Contracts package, I am still getting errors telling me that Windows does not exist in the Microsoft namespace. This is true for .NET framework 4.7.2 which I am using right now and 4.6.1 which I briefly retargeted to to match the sample you provided. Any thoughts on why this is happening? All I need to do here is access the token cache.

jmprieur commented 3 years ago

@leskodan : I would suggest you look at the csproj of the WPF one. cc; @bgavrilMS @trwalke @pmaytak to help

leskodan commented 3 years ago

@jmprieur @bgavrilMS @trwalke @pmaytak I am trying to modify this active-directory-b2c-dotnet-desktop sample to work once packaged and distributed. Therefore, the csproj file does not have any reference to the token cache issue.

CacheFilePath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalCacheFolder.Path, ".msalcache.bin3");

Using the application data location for packaged msix apps is not working for me either as I cannot get any of the Windows apis to work in this sample, even with Microsoft.Windows.SDK.Contracts installed. I get the "Windows does not exist in the namespace Microsoft" error.

Any ideas for a workaround or alternative approach?

leskodan commented 3 years ago

Got it! I was missing the step of converting my default package management to package reference. Thanks for everyone's help!

SteffenCleveland commented 2 years ago

Try and run the application as an administrator. Then the access to the folder should no longer be denied.