我使用本教程为我的网站配置了Twitch 身份验证: 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);
}
}