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

对着背影说爱祢 提交于 2019-12-10 20:02:03

问题


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.


回答1:


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?



来源:https://stackoverflow.com/questions/34067530/appending-hd-parameter-to-redirecturl-asp-net-core-1-with-identity-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!