问题描述
我使用本教程为我的网站配置了 Twitch authentication:https://blog.elmah.io/cookie-authentication-with-social-providers-in-asp-net-core/ 在 Twitch 开发控制台中,我将 https://localhost:44348/signin-twitch url 添加到回调 url 中。我之前为其他提供商实现了 oauth,但在使用 Twitch 时遇到了问题。
当我尝试登录时,我会看到 Twitch 授权屏幕,但在单击“授权”后,它会将我重定向到 /signin-twitch + params,它返回 Correlation Failed
异常。
Exception: An error was encountered while handling the remote login.
我觉得这可能与路由有关。它的设置是这样的,因为我有一个前端应用程序,它有自己的路由(因此是回退)
这是所有相关代码。
public void ConfigureServices(IServiceCollection services)
{
...
services.AddAuthentication(options =>
{
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme,options =>
{
options.LoginPath = "/signin";
options.logoutPath = "/signout";
})
.AddTwitch(TwitchAuthenticationDefaults.AuthenticationScheme,options =>
{
options.ClientId = "xxx";
options.ClientSecret = "xxx";
options.Scope.Add("user:read:email");
options.Savetokens = true;
options.AccessDeniedpath = "/";
});
}
public void Configure(IApplicationBuilder app,IWebHostEnvironment env)
{
...
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapFallbackToController("Index","Home");
});
}
public class AuthenticationController : Controller
{
[HttpGet("~/signin")]
public IActionResult SignIn(string returnUrl = "")
{
return Challenge(TwitchAuthenticationDefaults.AuthenticationScheme);
}
}
解决方法
我认为发生错误是因为您尝试访问分配为 Callback Path
的 URL。
试试这个的一些变体:
[HttpGet("~/signin")]
public IActionResult SignIn()
{
var authProperties = _signInManager
.ConfigureExternalAuthenticationProperties("Twitch",Url.Action("LoggingIn","Account",null,Request.Scheme));
return Challenge(authProperties,"Twitch");
}
来源:this answer 和 this one。
其他需要检查的内容:
- 具有相同
Callback Path
的多个客户端 - CookiePolicyOptions
- HTTPS 重定向