我正在尝试按照说明进行操作 here将 Cookie 身份验证添加到我的网站。
到目前为止,我添加了以下内容:
Invoke the UseAuthentication method in the Configure method of the Startup.cs file:
app.UseAuthentication();
Invoke the AddAuthentication and AddCookie methods in the ConfigureServices method of the Startup.cs file:
services.AddAuthentication("MyCookieAuthenticationScheme")
.AddCookie(options => {
options.AccessDeniedPath = "/Account/Forbidden/";
options.LoginPath = "/Account/Unauthorized/";
});
然后在我的登录代码中有
await HttpContext.SignInAsync("MyCookieAuthenticationScheme", principal);
principle 是一个 ClaimsPrincipal。
当我登录到我的网站并调用上面的行时,我得到了错误:
InvalidOperationException: No IAuthenticationSignInHandler is configured to handle sign in for the scheme: MyCookieAuthenticationScheme
我错过了什么?
最佳答案
您说您希望默认方案为“MyCookieAuthenticationScheme”(这是 AddAuthentication 的第一个参数)但您没有添加具有该名称的身份验证处理程序。当您调用 AddCookies 时,您添加了带有方案“Cookies”(这是默认设置)的处理程序。
您需要将代码更改为:
services.AddAuthentication("MyCookieAuthenticationScheme")
.AddCookie("MyCookieAuthenticationScheme", options =>
{
options.AccessDeniedPath = "/Account/Forbidden/";
options.LoginPath = "/Account/Unauthorized/";
});
请参阅这篇文章以更好地理解原语:
https://digitalmccullough.com/posts/aspnetcore-auth-system-demystified.html
关于c# - 无效操作异常 : No IAuthenticationSignInHandler is configured to handle sign in for the scheme: MyCookieAuthenticationScheme,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45776374/