Make ASP.NET Identity 2.0 Email Token Verification for WCF and MVC

I have a project project (WCF) and MVC that uses the same database to process the service part for the Mobile and Interface part. I have to set up an email confirmation for both.

I used the OWIN ASP.NET 2.0 library for authentication, and both projects have separate UserManagers.

For MVC

public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) { var dataProtectionProvider = options.DataProtectionProvider; if (dataProtectionProvider != null) { manager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>dataProtectionProvider.Create("ASP.NET")); } } 

For WCF

 var provider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("ASP.NET"); UserManager.UserTokenProvider =new Microsoft.AspNet.Identity.Owin.DataProtectorTokenProvider<ApplicationUser>( provider.Create("EmailConfirm")); var code = idManager.UserManager.GenerateEmailConfirmationToken(appuser.Id); 

Problem The email confirmation token generated in MVC works fine.

In WCF, when I create an email confirmation token, it must be verified from the MVC website. Here he gives me an "Invalid token."

I think this is due to the fact that the token codes do not match, I tried to make them the same as I can, but I don’t really know which one is between Wcf and MVC.

by the way. I am testing on localhost from Visual studio.

+4
source share
1 answer

I have found a solution. The problem was the DataProtectionProvider .

In MVC, he was getting:

 public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) { var dataProtectionProvider = **options.DataProtectionProvider**; 

In WCF, it was from:

 var provider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("ASP.NET"); 

So, I used the same thing as WCF in MVC:

 var dataProtectionProvider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("ASP.NET"); 

In addition, the key point:

  • The name of the provider . Both should be the same.
  • When creating the URL from WCF, we need to use HttpUtility.UrlEncode to encode the token code .

Example:

 var code = UserManager.GenerateEmailConfirmationToken(appuser.Id); var callbackUrl = string.Format("http://MVCSite/Account/ConfirmEmail?userId={0}&code={1}", HttpUtility.UrlEncode(appuser.Id), HttpUtility.UrlEncode(code)); 

Update

For those trying to host multiple projects, you need one of these two things to be the same in order to use the verification code:

  • Application pool
  • Car key

Email Verification Error Invalid AspNet Token ID

http://gunaatita.com/Blog/How-to-Generate-Machine-Key-using-IIS/1058

+6
source

Source: https://habr.com/ru/post/1241523/


All Articles