Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataProtectionProvider fails to unprotect when hosted on IIS with keys persisted to the default location #59461

Open
kikaragyozov opened this issue Dec 12, 2024 · 2 comments
Labels
area-dataprotection Includes: DataProtection

Comments

@kikaragyozov
Copy link

kikaragyozov commented Dec 12, 2024

When running ASP.NET Core on Kestrel/IIS Express, everything works as expected.

The following method gets called before the app is built:

        public static IDataProtectionProvider Create(string discriminator)
        {
            // build the service collection
            var serviceCollection = new ServiceCollection();
            var builder = serviceCollection.AddDataProtection()
                .SetApplicationName(discriminator)
                   .ProtectKeysWithDpapi(true);

            return serviceCollection.BuildServiceProvider().GetRequiredService<IDataProtectionProvider>();
        }

This works in IIS Express, Kestrel. I haven't tested this in docker, but I'm guessing because it uses Kestrel under the hood, it'll work as well.

The moment we move to using IIS though, all hell breaks lose. My Unprotect call fails with the error

System.Security.Cryptography.CryptographicException: The key {XXXXXX-XXXX-XXXX-XXXXX} was not found in the key ring. For more information go to https://aka.ms/aspnet/dataprotectionwarning

What's going on here? The IIS configured is running on my local machine, the same machine from which both the other methods worked.
Furthermore, I've manually added permissions to the default location of where the keys are being stored, verified that indeed the GUID mentioned EXISTS in the folder, i.e the file is there.

I've also tried specifying manually a folder, via PersistKeysToFileSystem which fixes the issue, but why doesn't it work via the default location (%LOCALAPPDATA%\ASP.NET\DataProtection-Keys)

Is there something specific that needs to be done on IIS for it to work out of the box?

Load User Profile is set to true in the Application pool.

Tested on .NET 9/.NET 8

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-dataprotection Includes: DataProtection label Dec 12, 2024
@kikaragyozov kikaragyozov changed the title DataProtectionProvider fails to unprotect when hosted on IIS and is set to use DPAPI DataProtectionProvider fails to unprotect when hosted on IIS with keys persisted to the default location Dec 12, 2024
@adityamandaleeka
Copy link
Member

Can you please take a look here: https://learn.microsoft.com/en-us/aspnet/core/security/data-protection/configuration/default-settings?view=aspnetcore-9.0#key-management

I wonder if the environment isn't flowing properly (see the part about setProfileEnvironment in that doc).

@kikaragyozov
Copy link
Author

        <applicationPools>
            <add name="DefaultAppPool" />
            <add name="rdf" autoStart="true" managedRuntimeVersion="" />
            <applicationPoolDefaults managedRuntimeVersion="v4.0">
                <processModel identityType="ApplicationPoolIdentity" loadUserProfile="true" setProfileEnvironment="true" />
            </applicationPoolDefaults>
        </applicationPools>

setProfileEnvironment was set to false. I changed it and restarted the IIS server from the IIS application window by clicking "Stop" and then "Start". The issue still occurs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-dataprotection Includes: DataProtection
Projects
None yet
Development

No branches or pull requests

2 participants