将 'hd' 参数附加到 redirectUrl ASP.NET Core 1 with Identity 3
Appending 'hd' parameter to redirectUrl ASP.NET Core 1 with Identity 3
在使用身份框架 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);
但是我找不到关于如何使用新的 ASP.NET Core 1 with Identity Framework 3 做同样事情的文档。
在 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);
}
};
});
但这是正确的方法吗,还是有更好的方法?
在使用身份框架 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);
但是我找不到关于如何使用新的 ASP.NET Core 1 with Identity Framework 3 做同样事情的文档。
在 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);
}
};
});
但这是正确的方法吗,还是有更好的方法?