在带有 Identity Framework 2 的 ASP.NET 4 中,我可以在 redirectUri 后附加一个我自己的参数,例如 Google 用来限制登录域的“hd”参数:
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);
但我无法找到有关如何使用带有身份框架 3 的新 ASP.NET Core 1 执行相同操作的文档。
最佳答案
在 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);
}
};
});
但这是执行此操作的正确方法,还是有更好的方法?
关于c# - 将 'hd' 参数附加到 redirectUrl ASP.NET Core 1 with Identity 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34067530/