2016-02-25 43 views
-1

我想封裝註冊和登錄邏輯到我的文件在MVC。 一切工作正常,但我總是得到'無效的令牌。'電子郵件確認程序:(錯誤後'令牌無效'。錯誤,而電子郵件確認ASP.NET MVC

請,你能幫忙嗎?我收到後搜索解決方案兩天瘋狂!

我注意到GenerateEmailConfirmationToken方法總是產生不同的令牌,因爲它是在日誌顯示??文件是否確定如果沒有存儲的令牌,它應該是始終不變的,正確的

這裏是我的倉庫構造函數編寫ASP.NET經理:

UserStore<MyUser> store = null; 
    UserManager<MyUser, string> userManager = null; 
    SignInManager<MyUser, string> signInManager = null; 
    IAuthenticationManager authenticationManager = null; 
    AbstractLogger logger = AbstractLogger.GetInstance(); 

public MyUserRepository(MyDbContext context) : base(context) 
     { 
      store = new UserStore<MyUser>(context); 
      userManager = new UserManager<MyUser>(store); 

      authenticationManager = HttpContext.Current.GetOwinContext().Authentication; 
      signInManager = new SignInManager<MyUser, string>(userManager, authenticationManager); 

      userManager.PasswordValidator = new PasswordValidator() 
      { 
       RequireDigit = false, 
       RequiredLength = 6, 
       RequireLowercase = false, 
       RequireNonLetterOrDigit = false, 
       RequireUppercase = false 
      }; 

      userManager.UserValidator = new UserValidator<MyUser>(userManager) 
      { 
       RequireUniqueEmail = true, 
       AllowOnlyAlphanumericUserNames = true 
      }; 

      // Configure user lockout defaults 
      userManager.UserLockoutEnabledByDefault = true; 
      userManager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5); 
      userManager.MaxFailedAccessAttemptsBeforeLockout = 5; 

      userManager.EmailService = new EmailService(); 


      var provider = new DpapiDataProtectionProvider("MyApp.org"); 
      userManager.UserTokenProvider = new DataProtectorTokenProvider<MyUser, string>(provider.Create("UserToken")) as IUserTokenProvider<MyUser, string>; 
     } 

這裏是事登記N法:

public void Register(MyUser user, string password) 
     { 
      IdentityResult result = userManager.Create(user, password); 

      //For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 
      //Send an email with this link 
      string code = string.Empty; 

      for (int i = 0; i < 10; i++) 
      { 
       code = userManager.GenerateEmailConfirmationToken(user.Id); 
       logger.LogInfo("USER: " + user.Id + " CODE: " + code); 
      } 


      logger.LogInfo(
       string.Format(
       "New user {0}; {1}; {2}", 
       user.Email, 
       user.Id, 
       code 
       )); 


      string callbackUrl = string.Format("http://localhost:5320/Account/ConfirmEmail?userId={0}&code={1}", user.Id, code); // }, protocol: HttpContext.Current.Request.Url.Scheme); 

      userManager.SendEmail(user.Id, "Potvrzení registrace", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); 
     } 

這裏是確認方法:

public IdentityResult ConfirmEmail(string userId, string code) 
      { 
       return userManager.ConfirmEmail(userId, code); 
      } 

這裏是日誌輸出:

USER: 1371ccfd-e8fd-46ed-8bfb-9d51f68aca63 

CODE: AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAADOWwoIJFDEqzZ8IvsDocNQAAAAACAAAAAAAQZgAAAAEAACAAAABakUsHl8hIMJX5U5sOc4zEgxUY8ikanoiKoZyIJkttZgAAAAAOgAAAAAIAACAAAADeBcyc9fhA+UR93KdPyWf8zzbJJjAcleIzf4CHCTr3OmAAAABUmOOqZs1FhaSRTcT2gV4V7JRhXNqYuJxJzB0gbo5DDfX1d010qH7YYNe4+iBh6JwdpKXR4tmsPKpojUx3RyPTbKIU8X39CJGqeWAFXAnDZMWKH2ztSn5M5h8V1zrotZRAAAAAVbRUJlIZeKgN/FH5//NQWRBFqKc9GSq0TvMWkYgZeAOyIfTh+JAMoXA4FrYnmJswLZC44zmlZPdisKnsT81ArA== 

USER: 1371ccfd-e8fd-46ed-8bfb-9d51f68aca63 

CODE: AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAADOWwoIJFDEqzZ8IvsDocNQAAAAACAAAAAAAQZgAAAAEAACAAAABwzJP5VCa/GBicSwTV4Jwu2kt3XvX3xeklIFeJPiYB5QAAAAAOgAAAAAIAACAAAACfIv9bgzQJ9gwyc6Qhn/ml5iQU2qgvO83RiQGbEK/U32AAAACTT2WpFg2BdwLZzWI033SeNK3rUckzxkFkbeFGY7LlkuOhnrjsg/IMyv5YM8sFst8her1bPFi0NDvheSdIWIzWtBQFQZi2VuHRZz3+RiLQllIT/OS/94f1h+yx93QzIGhAAAAAVvPIboy1DrTKpv1easktkMW/olF+MT10MuNlQivcx5wDUSuvzql5GM6GY87Nkm1lFzp9+n0XNWEpbFRqilBuMA== 
+0

您需要關閉後驗證 –

+0

如何?那是什麼? – Tom

+0

http://stackoverflow.com/questions/25405307/asp-net-identity-2-invalid-token-error和http://stackoverflow.com/questions/27535233/aspnet-identity-invalid-token-on-confirmation - 電子郵件 –

回答

0

感謝。問題真的在編碼。代碼不可能每次都是一樣的。

0

請使用Url.Action,而不是使用字符串連接創建網址。 Url.Action在最新的MVC版本中進行幕後編碼,您可以避免編碼和解碼操作。

下面是您可以使用的代碼片段。

string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

    var callbackUrl = Url.Action("ConfirmEmail", "Account", new 
        { 
         userId = user.Id, 
         code = code, 
         returnUrl = model.ReturnUrl 
        }, protocol: Request.Url.Scheme); 

如果你不想使用Url.Action那麼你可以使用HttpUtility.UrlEncode(code);編碼令牌而產生的URL

相關問題