如何在AddFacebook AspNetCore认证方案中正确设置auth_type参数?



要强制您网站上的用户(已经通过身份验证(通过 Facebook 提供商每次都重新进行身份验证,我们需要提供查询字符串参数,例如auth_type=reauthenticate.

注意:只需在对象上设置SetParameter("prompt", "consent")即可对Google进行相同的行为AuthenticationProperties但对于Facebook来说,这是行不通的。

一种方法是自己处理参数。 首先,我们在发出质询时添加参数:

var props = new AuthenticationProperties
{
RedirectUri = "/"
};
props.SetParameter("AuthType", "reauthenticate");
return Challenge(props);

然后我们定义一个在重定向之前调用的回调函数:

.AddFacebook(o =>
{
o.AppId = "app-id";
o.AppSecret = "app-secret";
o.Events = new Microsoft.AspNetCore.Authentication.OAuth.OAuthEvents
{
OnRedirectToAuthorizationEndpoint = ctx =>
{
if (ctx.Properties.Parameters.TryGetValue("AuthType", out object authTypeObj) && authTypeObj is string authType)
{
ctx.RedirectUri = QueryHelpers.AddQueryString(ctx.RedirectUri, "auth_type", authType);
}
ctx.Response.Redirect(ctx.RedirectUri);
return Task.CompletedTask;
}
};
});

请注意,我们必须在最后发出重定向。 默认处理程序这样做, 但是定义我们自己的处理程序会覆盖它。

最新更新