2016-01-16 66 views

回答

0

不,你不能通過自己替換完整的ASP.NET身份代碼。令牌根據RFC 6238生成並驗證,使用6位數字。你可以參考源代碼here

0

是的,你可以重新實現GenerateChangePhoneNumberToken,因爲它被定義爲Usermanager類中的一個虛擬方法。 順便說一下,您必須更改類Rfc6238Authentication中的代碼來自定義已編碼的主要功能,但不能更改生成的代碼。 爲了達到這個目的,你應該把this課程和(我的意思是複製/粘貼)到你的項目中,然後根據你的需要進行修改。 我想你可能需要改變otp的生活時間。我做過了。

爲線索: 在rfc6238的實施有一個名爲_timeStep用3分鐘默認值的私有靜態字段:

private static readonly TimeSpan _timestep = TimeSpan.FromMinutes(3); 

你可以它的行爲改變爲一個屬性(只讀去掉):

private static TimeSpan _timestep = TimeSpan.FromMinutes(3); 

並使GenerateCode方法更加動態:

public static int GenerateCode(SecurityToken securityToken, TimeSpan timeStep ,string modifier = null) 
     { 
      if (securityToken == null) 
      { 
       throw new ArgumentNullException("securityToken"); 
      } 
      _timestep = timeStep; 
      // Allow a variance of no greater than 90 seconds in either direction 
      var currentTimeStep = GetCurrentTimeStepNumber(); 
      using (var hashAlgorithm = new HMACSHA1(securityToken.GetDataNoClone())) 
      { 
       return ComputeTotp(hashAlgorithm, currentTimeStep, modifier); 
      } 
     } 

記住你必須將timeStep傳遞給ValidateCode方法,以確保驗證正常工作

+0

如果你已經這樣做了,爲什麼不分享你的代碼作爲例子?激勵'你可以做到這一點'很好,但不是一個真正的答案。 –

+0

我打算公佈我做過的漏洞,但是我已經爲一家公司做過這些,我必須請求允許分享我的代碼。 但你是對的,我可以通過一個例子來展示。我將編輯我的文章 –