Getting the email from external providers Google and Facebook during account association step in a default MVC5 app

PLEASE SEE UPDATES AT THE BOTTOM OF THIS POST! The following works for me for Facebook: StartupAuth.cs: var facebookAuthenticationOptions = new FacebookAuthenticationOptions() { AppId = “x”, AppSecret = “y” }; facebookAuthenticationOptions.Scope.Add(“email”); app.UseFacebookAuthentication(facebookAuthenticationOptions); ExternalLoginCallback method: var externalIdentity = HttpContext.GetOwinContext().Authentication.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie); var emailClaim = externalIdentity.Result.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Email); var email = emailClaim.Value; And for Google: StartupAuth.cs app.UseGoogleAuthentication(); … Read more

No IUserTokenProvider is registered

You have to specify a UserTokenProvider to generate a token. using Microsoft.Owin.Security.DataProtection; using Microsoft.AspNet.Identity.Owin; // … var provider = new DpapiDataProtectionProvider(“SampleAppName”); var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>()); userManager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>( provider.Create(“SampleTokenName”)); You should also read this article: Adding Two Factor Authentication to an Application Using ASP.NET Identity.

Register IAuthenticationManager with Simple Injector

As TrailMax already mentioned, the exception you got probably got raised during the call to container.Verify(). At application start-up time there is no HttpContext, hence the exception. Although the removal of the call to container.Verify() will ‘solve’ the problem, I would advise against doing this and I will suggest a better solution below. NightOwl888 references … Read more

How do I forcefully propagate role changes to users with ASP.NET Identity 2.0.1?

If you want to enable immediate revocation of cookies, then every request must hit the database to validate the cookie. So the tradeoff between delay is with your database load. But you can always set the validationInterval to 0. app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, LoginPath = new PathString(“/Account/Login”), Provider = new CookieAuthenticationProvider { // … Read more

Asp.NET Identity 2 giving “Invalid Token” error

I encountered this problem and resolved it. There are several possible reasons. 1. URL-Encoding issues (if problem occurring “randomly”) If this happens randomly, you might be running into url-encoding problems. For unknown reasons, the token is not designed for url-safe, which means it might contain invalid characters when being passed through a url (for example, … Read more

Why new fb api 2.4 returns null email on MVC 5 with Identity and oauth 2?

Taken from a Katana Thread I devised the following: Change the FacebookAuthenticationOptions to include BackchannelHttpHandler and UserInformationEndpoint as seen below. Make sure to include the names of the fields you want and need for your implementation. var facebookOptions = new FacebookAuthenticationOptions() { AppId = “*”, AppSecret = “*”, BackchannelHttpHandler = new FacebookBackChannelHandler(), UserInformationEndpoint = “https://graph.facebook.com/v2.4/me?fields=id,name,email,first_name,last_name” … Read more

How to extend available properties of User.Identity

Whenever you want to extend the properties of User.Identity with any additional properties like the question above, add these properties to the ApplicationUser class first like so: public class ApplicationUser : IdentityUser { public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) { // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType var userIdentity = await manager.CreateIdentityAsync(this, … Read more