追加' hd' redirectUrl ASP.NET Core 1与Identity 3的参数

时间:2015-12-03 13:39:18

标签: c# asp.net asp.net-identity google-oauth asp.net-core

在带有Identity Framework 2的ASP.NET 4中,我可以使用我自己的参数附加redirectUri,例如' hd' Google用来限制登录域名的参数:

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);

但我无法找到有关如何使用Identity Framework 3的新ASP.NET Core 1执行相同操作的文档。

1 个答案:

答案 0 :(得分:2)

我在GitHub的源代码的帮助下,作为一个工作解决方案,非常像问题中的那个解决方案,如下:

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);
        }
    };
});

但这是正确的方法,还是有更好的方法?