Adding a hosted domain parameter is not directly supposed through the GoogleOptions class, unfortunately – however with a few (not obvious) tweaks, you can easily add the parameter!
In your Startup.cs, set up a new Events parameter as such:
services.AddAuthentication().AddGoogle(googleOptions =>
{
const string hostedDomain = "askgif.com";
googleOptions.ClientId = Configuration["Google:ClientId"];
googleOptions.ClientSecret = Configuration["Google:ClientSecret"];
googleOptions.Events = new OAuthEvents
{
OnRedirectToAuthorizationEndpoint = context =>
{
context.Response.Redirect(context.RedirectUri + "&hd=" + System.Net.WebUtility.UrlEncode(hostedDomain));
return Task.CompletedTask;
},
OnCreatingTicket = context =>
{
if (context.User.Value("domain") != hostedDomain)
{
throw new AuthenticationException($"You must sign in with a {hostedDomain} email address");
}
return Task.CompletedTask;
}
};
});
It's as simple as it is explained above. :)



