with password net mvc google example asp and asp.net-mvc asp.net-identity email-confirmation

asp.net-mvc - mvc - reset password asp.net identity



Prueba de Identity Framework si el token de confirmación de correo electrónico ha caducado (3)

¿Es posible probar si un token de correo electrónico de confirmación ha caducado con el UserManager de UserManager Identity Framework? No importa cuál sea el error, de lo siguiente:

var result = await UserManager.ConfirmEmailAsync(userId, code);

Recibo un error genérico "Token no válido".


Aquí viene una adaptación de .NET Core 2.1 de la solución provista por @Nkosi:

Clase ApplicationUser

public class ApplicationUser : IdentityUser { public string EmailConfirmationToken { get; set; } public string ResetPasswordToken { get; set; } }

Clase de UserManager derivada

public class CustomUserManager : UserManager<ApplicationUser> { public CustomUserManager(IUserStore<ApplicationUser> store, IOptions<IdentityOptions> optionsAccessor, IPasswordHasher<ApplicationUser> passwordHasher, IEnumerable<IUserValidator<ApplicationUser>> userValidators, IEnumerable<IPasswordValidator<ApplicationUser>> passwordValidators, ILookupNormalizer keyNormalizer, IdentityErrorDescriber errors, IServiceProvider services, ILogger<UserManager<ApplicationUser>> logger) : base(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, services, logger) { } public override async Task<string> GenerateEmailConfirmationTokenAsync(ApplicationUser user) { /* NOTE: * The default UserTokenProvider generates tokens based on the users''s SecurityStamp, so until that changes * (like when the user''s password changes), the tokens will always be the same, and remain valid. * So if you want to simply invalidate old tokens, just call manager.UpdateSecurityStampAsync(). */ //await base.UpdateSecurityStampAsync(userId); var token = await base.GenerateEmailConfirmationTokenAsync(user); if (!string.IsNullOrEmpty(token)) { user.EmailConfirmationToken = token; //<<< Last issued token //Note: If a token is generated then the current email is no longer confirmed. user.EmailConfirmed = false; await UpdateAsync(user); } return token; } public override async Task<IdentityResult> ConfirmEmailAsync(ApplicationUser user, string token) { if (user == null) { return IdentityResult.Failed(new IdentityError {Description = "User not found."}); } var result = await base.ConfirmEmailAsync(user, token); if (result.Succeeded) { user.EmailConfirmationToken = null; return await UpdateAsync(user); } else if (user.EmailConfirmationToken == token) { //Previously Issued Token expired result = IdentityResult.Failed(new IdentityError { Description = "Expired token." }); } return result; } }

Extensión de UserManager

public static class ApplicationUserManagerExtension { public static Task<string> FindIdByEmailConfirmationTokenAsync(this UserManager<ApplicationUser> manager, string confirmationToken) { string result = null; ApplicationUser user = manager.Users .SingleOrDefault(u => u.EmailConfirmationToken != null && u.EmailConfirmationToken == confirmationToken); if (user != null) { result = user.Id; } return Task.FromResult(result); } }

Actualización: el CustomUserManager debe agregarse a los servicios en Startup.cs en el método ConfigureServices.

services.AddTransient<CustomUserManager>();

Sin esto, DependencyInjection falla.


Encontré una forma de analizar el token para la fecha de emisión, que luego puede verificar para ver si está dentro del período de tiempo permitido (valor predeterminado de 24 horas si no se especifica).

Identidad.cs

ApplicationUserManager

public IDataProtector Protector { get; set; } public TimeSpan TokenLifespan { get; set; }

ApplicationUserManager Create ()

// Explicitly set token expiration to 24 hours. manager.TokenLifespan = TimeSpan.FromHours(24); var dataProtectionProvider = options.DataProtectionProvider; manager.Protector = dataProtectionProvider.Create("ASP.NET Identity"); if (dataProtectionProvider != null) { manager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity")) { TokenLifespan = manager.TokenLifespan }; }

AccountController.cs

public async Task<ActionResult> ConfirmEmail(string Code, string UserId) { // Try/catch, validation, etc. var tokenExpired = false; var unprotectedData = UserManager.Protector.Unprotect(Convert.FromBase64String(Code)); var ms = new MemoryStream(unprotectedData); using (BinaryReader reader = new BinaryReader(ms)) { var creationTime = new DateTimeOffset(reader.ReadInt64(), TimeSpan.Zero); var expirationTime = creationTime + UserManager.TokenLifespan; if (expirationTime < DateTimeOffset.UtcNow) { tokenExpired = true; } } // Do something if token is expired, else continue with confirmation }

Considero que esta publicación del blog y la respuesta de Nkosi son extremadamente útiles, y si desea revisar el código fuente de Identity, Microsoft lo tiene here (las versiones anteriores de Identity para MVC5 y más abajo here ). Además, pido disculpas si no está bien para responder a una pregunta que usted mismo ha ofrecido, pero no pude evitar seguir buscando una solución mejor.


Evito esto guardando / almacenando una copia del token generado

public class ApplicationUser : IdentityUser { public string EmailConfirmationToken { get; set; } public string ResetPasswordToken { get; set; } }

y asociarlo con el usuario en UserManager<ApplicationUser> derivado UserManager<ApplicationUser> .

public override async System.Threading.Tasks.Task<string> GenerateEmailConfirmationTokenAsync(string userId) { /* NOTE: * The default UserTokenProvider generates tokens based on the users''s SecurityStamp, so until that changes * (like when the user''s password changes), the tokens will always be the same, and remain valid. * So if you want to simply invalidate old tokens, just call manager.UpdateSecurityStampAsync(). */ //await base.UpdateSecurityStampAsync(userId); var token = await base.GenerateEmailConfirmationTokenAsync(userId); if (!string.IsNullOrEmpty(token)) { var user = await FindByIdAsync(userId); user.EmailConfirmationToken = token; //<<< Last issued token //Note: If a token is generated then the current email is no longer confirmed. user.EmailConfirmed = false; await UpdateAsync(user); } return token; }

Cuando se proporciona el token para confirmación, se realiza una búsqueda del usuario a través del token.

public static class ApplicationUserManagerExtension { public static Task<string> FindIdByEmailConfirmationTokenAsync(this UserManager<ApplicationUser> manager, string confirmationToken) { string result = null; ApplicationUser user = manager.Users.SingleOrDefault(u => u.EmailConfirmationToken != null && u.EmailConfirmationToken == confirmationToken); if (user != null) { result = user.Id; } return Task.FromResult(result); } }

Si el token coincide con un usuario conocido, eso indica que fue un token emitido de forma válida.

Luego intentará confirmar el token con el administrador de usuarios.

Si la confirmación falla, el token ha caducado y se realiza una acción apropiada.

De lo contrario, si el token confirmó, se elimina del usuario asociado y, por lo tanto, se invalida la reutilización de ese token.

public override async System.Threading.Tasks.Task<IdentityResult> ConfirmEmailAsync(string userId, string token) { var user = await FindByIdAsync(userId); if (user == null) { return IdentityResult.Failed("User Id Not Found"); } var result = await base.ConfirmEmailAsync(userId, token); if (result.Succeeded) { user.EmailConfirmationToken = null; return await UpdateAsync(user); } else if (user.EmailConfirmationToken == token) { //Previously Issued Token expired result = IdentityResult.Failed("Expired Token"); } return result; }

También se implementó un enfoque similar para restablecer la contraseña.