Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm working on a small MVC5 website that makes use of Google authentication however I wanted to do it without the ASP.NET Identity.

I have so far followed the steps in this blog post and have a working login: http://coding.abel.nu/2014/11/using-owin-external-login-without-asp-net-identity/

However currently the User.Identity.Name is being set as the user's full name. I want to set it to the user's email address as I prefer to have it set to my user's primary key. My code consists so far solely on what is in that blog post.

I'm able to retrieve the user's email address with the following code but this is only after the user has authenticated.

ClaimsPrincipal.Current.FindFirst(ClaimTypes.Email).Value;

I have tried overriding the OnAuthenticated for the GoogleOAuth2ProviderOptions and found that there is a NameClaimType however it is readonly.

app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions
        {
            ClientId = "...",
            ClientSecret = "...",
            Provider = new GoogleOAuth2AuthenticationProvider()
            {
                OnAuthenticated = async context =>
                {
                    context.Identity.NameClaimType = ClaimTypes.Email;
                }
            }
        });

I haven't been able to find anything else that lets me set what the User.Identity.Name is set to. Any ideas?

share|improve this question
    
What do you store in the username field of ApplicationUser? Is your app based on the template app? If so have you modified Register method of the accountcontroller? –  cellik Mar 27 at 16:00
    
I'm not using ready-template that provides the ApplicationUser as I preferred to start my own. I started with an empty MVC project. –  Frelli Mar 27 at 16:04

2 Answers 2

In the general usage when the user registers with a Google account a new local account is created and linked to the Google login. There you need to create that user as below. (taken from the template)

 var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
                var result = await UserManager.CreateAsync(user);

Here, you need to set the username field to email for your needs

share|improve this answer

Think I fixed it. I created my own implementation of the IGoogleOAuth2AuthenticationProvider which removes the Name claim and re-adds it using the Email provided by Google. Not sure if this is the best way to do it but it works.

public class GoogleAuthProvider : IGoogleOAuth2AuthenticationProvider 
{
    public void ApplyRedirect(GoogleOAuth2ApplyRedirectContext context)
    {
        context.Response.Redirect(context.RedirectUri);
    }

    public System.Threading.Tasks.Task Authenticated(GoogleOAuth2AuthenticatedContext context)
    {
        context.Identity.RemoveClaim(context.Identity.FindFirst(ClaimTypes.Name));
        context.Identity.AddClaim(new Claim(ClaimTypes.Name, context.Email));
        context.Identity.AddClaim(new Claim(ClaimTypes.Email, context.Email));
        return Task.FromResult<object>(null);
    }

    public System.Threading.Tasks.Task ReturnEndpoint(GoogleOAuth2ReturnEndpointContext context)
    {
        return Task.FromResult<object>(null);
    }
}

And then hooked up the provider when setting up GoogleOauth

app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions
            {
                ClientId = "...",
                ClientSecret = "...",
                Provider = new GoogleAuthProvider()
            });
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.