Identity icon indicating copy to clipboard operation
Identity copied to clipboard

ASP.NET Core Identity in multiple ASP.NET Core Applications: Invalid Token

Open lurumad opened this issue 9 years ago • 6 comments

Hi,

I have two web applications, an administration app and Identity Server app. In the administration app, administrators create new users accounts and this app generates a token for email confirmation:

var token = await userManager.GenerateEmailConfirmationTokenAsync(user);

I want to confirm email and reset the password in the identity server app, but I always recieved an InvalidToken error.

I've configured Identity in both apps:

services.AddIdentity<User, IdentityRole>()
                .AddEntityFrameworkStores<AdministrationDbContext>()
                .AddDefaultTokenProviders();

If I generate and confirm the email in the administration app works, but in identity server app always the same error: InvalidToken

Did I missed something?

Regards!

lurumad avatar Nov 01 '17 10:11 lurumad

We are having the same issue but with the reset password tokens. When we look a the token going out and coming back in they are the exact same. Why is this failing?

chassq avatar Nov 04 '17 12:11 chassq

In my case, the tokens were the same length but weren't being encoded on the links being sent out, so the plus symbols for example were being replaced with spaces. Might be worth checking that.

AshleyPoole avatar Nov 06 '17 12:11 AshleyPoole

In my case, I'he tried to encode the tokens but anyway I always received "Invalid Token"

Regards!

lurumad avatar Nov 06 '17 12:11 lurumad

For us we made sure we were including the SecurityStamp (on the User) when generating and getting the token to compare. Once we did this we saw our issue resolve. I think there is a way to disable SecurityStamp checking on the SignInManager if you need. Look at https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.identity.signinmanager-1.validatesecuritystampasync?view=aspnetcore-2.0 and https://stackoverflow.com/questions/19487322/what-is-asp-net-identitys-iusersecuritystampstoretuser-interface

Hope this helps...

chassq avatar Nov 06 '17 15:11 chassq

services.AddIdentity<User, Role>(config => { config.Tokens.EmailConfirmationTokenProvider = TokenOptions.DefaultEmailProvider; }).AddEntityFrameworkStores()

atpyk avatar Nov 15 '17 01:11 atpyk

Hi folks,

I've resolved the problem. After debugging the source code of AspNetIdentity, I've seen that the problem is related with DataProtectionApi, and to be more precise with the IApplicationDiscriminator see. Uses a HostingApplicationDiscriminator that returns ContentRootPath of the applications and it's use to generated the keys. Another thing I've found is that you can persists the keys into files so you can reuse across your apps.

The solution in my case is using the same ApplicationDiscriminator in the two web apps and share the keys in Azure storage (You can use many storage like directory, UNC, Redis...) More info here

services
    .AddDataProtection(options =>
    {
            options.ApplicationDiscriminator = "myapp";
    })
    .PersistKeysToAzureBlobStorage(new Uri(sas));

And works like a charm!!!

Regards!

lurumad avatar Nov 15 '17 16:11 lurumad