Johan B
Johan B

Reputation: 890

Appending 'hd' parameter to redirectUrl ASP.NET Core 1 with Identity 3

In ASP.NET 4 with the Identity Framework 2 I can append the redirectUri with a parameter of my own, like the 'hd' parameter Google uses to limit login to a domain like this:

var googleAuthOptions = new GoogleOAuth2AuthenticationOptions
{
    ClientId = "redacted",
    ClientSecret = "redacted",
    Provider = new CustomGoogleProvider
    {
        OnApplyRedirect = context =>
        {
            var redirect = context.RedirectUri;
            redirect += "&hd=contoso.com";
            context.Response.Redirect(redirect);
        }
    }
};

app.UseGoogleAuthentication(googleAuthOptions);

But I'm unable to find documentation on how to do the same thing with the new ASP.NET Core 1 with Identity Framework 3.

Upvotes: 4

Views: 1201

Answers (1)

Johan B
Johan B

Reputation: 890

What I came up with as a working solution, very much like the one in the question, with the help of the source code on GitHub, is the following:

app.UseGoogleAuthentication(options => {
    options.ClientId = Configuration["Authentication:Google:ClientId"];
    options.ClientSecret = Configuration["Authentication:Google:ClientSecret"];

    options.Events = new OAuthEvents()
    {
        OnRedirectToAuthorizationEndpoint = context =>
        {
            context.Response.Redirect(context.RedirectUri + "&hd=contoso.com");
            return Task.FromResult(0);
        }
    };
});

But is this the correct way to do this, or is there a better way?

Upvotes: 3

Related Questions