在 Startup.cs 文件的 ConfigureServices 方法中添加 app.UseCookiePolicy 和 app.UseSession,并在 Startup.cs 文件的 Configure 方法中添加 app.UseAuthentication。示例代码如下:
在 ConfigureServices 方法中添加以下代码:
services.AddSession(options => { options.IdleTimeout = TimeSpan.FromMinutes(30); });
services.AddAuthentication(options => { options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme; options.DefaultChallengeScheme = TwitchAuthenticationDefaults.AuthenticationScheme; }) .AddCookie(options => { options.LoginPath = "/signin-twitch"; }) .AddTwitch(options => { options.ClientId = Configuration["Twitch:ClientId"]; options.ClientSecret = Configuration["Twitch:ClientSecret"]; options.Scope.Add("user:read:email"); });
app.UseCookiePolicy(); app.UseSession(); app.UseAuthentication();
在 Configure 方法中添加以下代码:
app.UseAuthentication();
然后,在 Twitch 开发者控制台中更新 Redirect URI,将其设置为 https://localhost:[port]/signin-twitch。例如,如果应用程序使用端口号 5000,则 Redirect URI 将设置为 https://localhost:5000/signin-twitch。
最后,启动应用程序,使用 Twitch 帐户登录,如果一切顺利,用户将被重定向到主页。