Skip to content

Unable to Confirm Email using Default Token Provider #24763

@Exagram

Description

@Exagram

Describe the bug

Goals

I want to force newly-registered users to verify their emails before using my site. I assume that I have to use UserManager.GenerateEmailConfirmationTokenAsync(), TokenOptions.EmailConfirmationTokenProvider, `UserManager.ConfirmEmailAsync(). However when I sequentially call these methods I always get back "invalid token".

Token Provider Mismatch?

Changing the EmailConfirmationTokenProvider = "Email" works?

In startup.cs I added:

services.AddIdentity<CustomUser, CustomRole>(options => {
    options.Tokens.EmailConfirmationTokenProvider = TokenOptions.DefaultEmailProvider;
});
  • Instead of 200-char hex verification codes I get back a 6-digit number. I am able to confirm my email! However I want to understand the reason for why Identity is failing and second use the default 200-char hex verification codes.

Note: I've switched between making all my services/repo's transient and singletones and that didn't help.

I've found the documentation (https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity-custom-storage-providers?view=aspnetcore-3.1 and https://docs.microsoft.com/en-us/aspnet/core/security/authentication/accconfirm?view=aspnetcore-3.1&tabs=visual-studio#scaffold-registerconfirmation) to not have enough details to solve these above mentioned ambiguities.

To Reproduce

Startup.cs:

//ConfigureServices():
services.AddSingleton<IPasswordHasher<CustomUser>, PasswordHasher<CustomUser>>();
services.AddSingleton<ISqlConnectionFactory, SqlConnectionFactory>();
services.AddSingleton<IUserRepository, UserRepository>();
services.AddSingleton<IRoleStore<CustomRole>, CustomRoleStore>();
services.AddSingleton<IUserStore<CustomUser>, CustomUserStore>();
services.AddControllers();
services.AddIdentity<CustomUser, CustomRole>().AddDefaultTokenProviders();
services.ConfigureApplicationCookie(options => {...});
services.AddAuthorization();
//Configure():
builder.UseAuthentication();
builder.UseAuthorization();
builder.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllers();
                });

CustomUserStore:

public class CustomUserStore : IUserStore<CustomUser>, IUserPasswordStore<CustomUser>, IUserRoleStore<CustomUser>, IUserEmailStore<CustomUser>, IUserSecurityStampStore<CustomUser> { ... }

CustomRoleStore:

public class CustomRoleStore : IRoleStore<CustomRole>, IRoleClaimStore<CustomRole>, IDisposable { ... }

Exceptions (if any)

"Invalid token"

Further technical details

  • ASP.NET Core version 3.1.6
  • Include the output of dotnet --info
.NET Core SDK (reflecting any global.json):
 Version:   3.1.302
 Commit:    41faccf259

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.18363
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\3.1.302\

Host (useful for support):
  Version: 3.1.6
  Commit:  3acd9b0cd1

.NET Core SDKs installed:
  2.1.700 [C:\Program Files\dotnet\sdk]
  2.2.300 [C:\Program Files\dotnet\sdk]
  3.1.302 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.20 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.20 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.20 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  • The IDE (VS / VS Code/ VS4Mac) you're running on, and it's version
    VS 2019 Community Latest

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions