简体   繁体   English

撤销 ASP.NET Identity 2.0 中 UserTokenProvider 生成的令牌

[英]Revoke token generated by UserTokenProvider in ASP.NET Identity 2.0

Is there a way to revoke for example an email conformation token generated by an usermanager in ASP NET Identity 2.0?有没有办法撤销例如由用户管理器在 ASP NET Identity 2.0 中生成的电子邮件确认令牌?

Context语境
I would like to give the user the possibility to resend an confirmation email.我想让用户可以重新发送确认电子邮件。 To do this I generate a new token with: UserManager.GenerateEmailConfirmationTokenAsync(user.Id) , and send an email with the new generated token.为此,我使用以下命令生成新令牌: UserManager.GenerateEmailConfirmationTokenAsync(user.Id) ,并使用新生成的令牌发送电子邮件。 Unfortunately when I do this the previously generated tokens are still working, is there a way to revoke them?不幸的是,当我这样做时,以前生成的令牌仍在工作,有没有办法撤销它们?

Example code示例代码
In the UserManager class:在 UserManager 类中:

manager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(options.DataProtectionProvider.Create("ASP.NET Identity"));

In the AccountController:在 AccountController 中:

var user = await UserManager.FindByEmailAsync("email");

// All generated tokens below will work to confirm the email. 
// I only want the last token to be valid when confirming the email address.
var token1 = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
var token2 = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
var token3 = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
var token4 = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
var token5 = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

var result = await UserManager.ConfirmEmailAsync(user.Id, token5);

Information about the storage location of the generated token and how these tokens are generated are also welcome!也欢迎提供有关生成的令牌的存储位置以及这些令牌是如何生成的信息!

I will be grateful if you can send me this information.如果你能把这些信息发给我,我将不胜感激。

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.默认的 UserTokenProvider 会根据用户的 SecurityStamp 生成令牌,因此在更改之前(例如用户密码更改时),令牌将始终相同并保持有效。 So if you want to simply invalidate old tokens, just call因此,如果您只想使旧令牌无效,只需调用

manager.UpdateSecurityStampAsync();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM