How to set hosted domain in MVC GoogleOAuth2 Authentication Options

148 views Asked by At

I'm trying to allow only users on a specific domain ([email protected]) to log in to my system using their google accounts. I have read elsewhere that this can be accomplished by setting the hosted domain (hd). Here is the code that I have right now in my Startup.Auth.cs:

var googleOptions = new GoogleOAuth2AuthenticationOptions()
        {
            ClientId = "XXXXXXXXX.apps.googleusercontent.com",
            ClientSecret = "XXXXXXXXXXXX",
            Provider = new GoogleOAuth2AuthenticationProvider()
            {
                OnAuthenticated = (context) =>
                {
                    context.Identity.AddClaim(new Claim("urn:google:name", context.Identity.FindFirstValue(ClaimTypes.Name)));
                    context.Identity.AddClaim(new Claim("urn:google:email", context.Identity.FindFirstValue(ClaimTypes.Email)));

                    return Task.FromResult(0);
                }
            }
        };

        app.UseGoogleAuthentication(googleOptions);

Is there a way to add the hosted domain to the code that I am currently using? Should I be trying to do this somewhere else in my code? Thank you.

0

There are 0 answers