我正在使用Asp.Net身份。當我想通過電子郵件確認令牌確認新用戶時,系統地出現無效令牌錯誤。ASP.NET身份電子郵件確認無效令牌
這裏是我的WebAPI用戶控制器:
public class UsersController : ApiController
{
private MyContext _db;
private MyUserManager _userManager;
private MyRoleManager _roleManager;
public UsersController()
{
_db = new MyContext();
_userManager = new MyUserManager(new UserStore<MyUser>(_db));
_roleManager = new MyRoleManager(new RoleStore<IdentityRole>(_db));
}
//New user method
[HttpPost]
public async Task<HttpResponseMessage> Register([FromBody]PostUserModel userModel)
{
//New user code
...
var token = await _userManager.GenerateEmailConfirmationTokenAsync(user.Id);
var message = new IdentityMessage();
message.Body = string.Format("Hi {0} !\r\nFollow this link to set your password : \r\nhttps://www.mywebsite.com/admin/users/{1}/reset?token={2}", user.UserName, user.Id, HttpUtility.UrlEncode(token));
message.Subject = "Confirm e-mail";
message.Destination = user.Email;
await _userManager.SendEmailAsync(user.Id, message.Subject, message.Body);
return Request.CreateResponse(HttpStatusCode.OK, res); }
}
要確認的電子郵件,我只是在做:
var result = await _userManager.ConfirmEmailAsync(user.Id, token);
我不這樣做的HttpUtility.UrlDecode因爲的WebAPI本身做它。
我的自定義用戶管理器類:
public class MyUserManager : UserManager<MyUser>
{
public MyUserManager(IUserStore<MyUser> store)
: base(store)
{
UserValidator = new UserValidator<MyUser>(this)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
// Configure validation logic for passwords
PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = true,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
};
// Configure user lockout defaults
UserLockoutEnabledByDefault = false;
EmailService = new EmailService();
SmsService = new SmsService();
var dataProtectionProvider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("MyApp");
UserTokenProvider = new Microsoft.AspNet.Identity.Owin.DataProtectorTokenProvider<MyUser>(dataProtectionProvider.Create("UserToken"));
}
}
}
任何想法? 非常感謝
我在我的網站上有類似的問題。基本上DPADI不能在多臺機器上加密/解密。我不得不使用機器密鑰來完成我想要的。你可以在這裏閱讀更多關於它http://stackoverflow.com/a/23661872/1341538 –
謝謝你的回答。我讀過這個,但不幸的是,我在本地主機也遇到了這個問題。 –
可能有一堆原因。我已經寫了關於此之前:http://tech.trailmax.info/2015/05/asp-net-identity-invalid-token-for-password-reset-or-email-confirmation/ – trailmax