2016-01-21 66 views
2

有沒有什麼方法可以在中看到生成的密碼重置令牌?ASP.NET身份證2
我的假設是在TokenProvider應該有一個字典,它在內存中保存這些標記。在哪裏可以找到生成的PasswordReset令牌在ASP.NET身份2?

更新:
這裏有一個類似的問題ResetPassword Token How and where is it stored?

「令牌使用SecurityStamp和驗證針對 的SecurityStamp而不是隨時隨地存儲在數據庫或本地文件存儲 產生的。」

但問題仍然是框架如何知道令牌過期而不存儲它?

+0

令牌提供程序允許您創建重置令牌。您將不得不手動將其與相關用戶一起存儲。 – Nkosi

+0

@Nkosi我能夠生成令牌並使用它,但是我需要在內存中查看已經生成的令牌以用於調試目的。我認爲它是自動化的,作爲庫的一部分,我不需要將它存儲在任何地方 – Ehsan

+0

令牌提供程序不會將生成的代碼保存到內存中。 – Nkosi

回答

2

令牌不存儲在內存中,而是從給定的SecurityStamp中動態生成。令牌基本上是一個Unicode編碼的字符串。

你可以看到UserManager.csCreateSecurityTokenAsync方法。

internal async Task<byte[]> CreateSecurityTokenAsync(TUser user) 
{ 
    return Encoding.Unicode.GetBytes(await GetSecurityStampAsync(user)); 
} 
1

作爲由評議

之一提到的「令牌使用SecurityStamp生成並抵靠SecurityStamp驗證和不存儲在數據庫或本地文件存儲」。

這是從ResetPassword Token How and where is it stored?

這裏引用是我如何能夠堅持令牌以備後用。

我假設你正在使用默認的項目模板。

ApplicationUser創建屬性來存儲令牌。

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

AspNetUsers表創建相應列。

然後我更新了ApplicationUserManager類如下。

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); 
     //associate the email token with the user account 
     if (!string.IsNullOrEmpty(token)) { 
      var x = await FindByIdAsync(userId); 
      x.EmailConfirmationToken = token; 
      x.EmailConfirmed = false; 

      await UpdateAsync(x); 
     } 

     return token; 
    } 

    public override async System.Threading.Tasks.Task<string> GeneratePasswordResetTokenAsync(string userId) { 
     var token = await base.GeneratePasswordResetTokenAsync(userId); 
     if (!string.IsNullOrEmpty(token)) { 
      var x = await FindByIdAsync(userId); 
      x.ResetPasswordToken = token; 
      await UpdateAsync(x); 
     } 
     return token; 
    } 

    public override async System.Threading.Tasks.Task<IdentityResult> ConfirmEmailAsync(string userId, string token) { 
     var result = await base.ConfirmEmailAsync(userId, token); 
     if (result.Succeeded) { 
      var x = await FindByIdAsync(userId); 
      x.EmailConfirmationToken = null; 
      await UpdateAsync(x); 
     } 
     return result; 
    } 

    public override async System.Threading.Tasks.Task<IdentityResult> ResetPasswordAsync(string userId, string token, string newPassword) { 
     var result = await base.ResetPasswordAsync(userId, token, newPassword); 
     if (result.Succeeded) { 
      var x = await FindByIdAsync(userId); 
      x.ResetPasswordToken = null; 
      await UpdateAsync(x); 
     } 
     return result; 
    } 

上述更改允許我在會話之間持續使用令牌。

+0

這不是答案,因爲我已經知道如何使用該庫,但我需要知道TokenProvider如何以及在哪裏存儲令牌PasswordReset – Ehsan

+1

我發現這有助於擴展性的立場,我們討厭不得不要求我們的用戶輸入電子郵件地址(再次),以便「使用」重置鏈接。因此,存儲上次創建的代碼並通過它查找用戶會有很大幫助。注意:我們可能會在頑固的安全沙皇手中失去一些點,但是從UX專業人士那裏賺取它(一種平衡行爲)。 – timmi4sa

相關問題