/// <summary> /// Generate a token for the user using their security stamp /// </summary> /// <param name="purpose"></param> /// <param name="manager"></param> /// <param name="user"></param> /// <returns></returns> public virtual async Task <string> GenerateAsync(string purpose, UserManager <TUser, TKey> manager, TUser user) { var token = await manager.CreateSecurityTokenAsync(user.Id).WithCurrentCulture(); var modifier = await GetUserModifierAsync(purpose, manager, user).WithCurrentCulture(); return(Rfc6238AuthenticationService.GenerateCode(token, modifier).ToString("D6", CultureInfo.InvariantCulture)); }
/// <summary> /// Generate a token for the user using their security stamp /// </summary> /// <param name="purpose"></param> /// <param name="manager"></param> /// <param name="user"></param> /// <returns></returns> public virtual async Task <string> GenerateAsync(string purpose, UserManager <TUser> manager, TUser user) { if (manager == null) { throw new ArgumentNullException("manager"); } var token = await manager.CreateSecurityTokenAsync(user); var modifier = await GetUserModifierAsync(purpose, manager, user); return(Rfc6238AuthenticationService.GenerateCode(token, modifier).ToString("D6", CultureInfo.InvariantCulture)); }
/// <summary> /// Validate the token for the user /// </summary> /// <param name="purpose"></param> /// <param name="token"></param> /// <param name="manager"></param> /// <param name="user"></param> /// <returns></returns> public virtual async Task <bool> ValidateAsync(string purpose, string token, UserManager <TUser, TKey> manager, TUser user) { int code; if (!Int32.TryParse(token, out code)) { return(false); } var securityToken = await manager.CreateSecurityTokenAsync(user.Id).WithCurrentCulture(); var modifier = await GetUserModifierAsync(purpose, manager, user).WithCurrentCulture(); return(securityToken != null && Rfc6238AuthenticationService.ValidateCode(securityToken, code, modifier)); }
/// <summary> /// Validate the token for the user /// </summary> /// <param name="purpose"></param> /// <param name="token"></param> /// <param name="manager"></param> /// <param name="user"></param> /// <returns></returns> public virtual async Task <bool> ValidateAsync(string purpose, string token, UserManager <TUser> manager, TUser user) { if (manager == null) { throw new ArgumentNullException("manager"); } int code; if (!int.TryParse(token, out code)) { return(false); } var securityToken = await manager.CreateSecurityTokenAsync(user); var modifier = await GetUserModifierAsync(purpose, manager, user); return(securityToken != null && Rfc6238AuthenticationService.ValidateCode(securityToken, code, modifier)); }