Title

Saturday, 7 February 2015

How do you use the Confirm Email functionality in ASP.NET Identity 2.0


Identity 2.0 boasts new functionality in this area, but there's no documentation.

Answer

I found a couple of scraps of sample code. They were sufficiently obscure that I decided this posting would be useful. This is how you generate a code and a callback URL:

string code = manager.GenerateEmailConfirmationToken(user.Id);  string callbackUrl = IdentityHelper.GetUserConfirmationRedirectUrl(code, user.Id);  manager.SendEmail(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>.");

This is how you handle it on the return:

string code = IdentityHelper.GetCodeFromRequest(Request);  string userId = IdentityHelper.GetUserIdFromRequest(Request);  if (code != null && userId != null)   {   var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();   var result = manager.ConfirmEmail(userId, code);

.... References: https://aspnet.codeplex.com/SourceControl/latest#Samples/Identity/Webforms.Samples/Webforms.Samples/Account/Confirm.aspx.cs

https://aspnet.codeplex.com/SourceControl/latest#Samples/Identity/Webforms.Samples/Webforms.Samples/Account/Register.aspx.cs

No comments:

Post a Comment