MyCookieAuthenticationScheme

MyCookieAuthenticationScheme

我正在尝试按照here的说明将Cookie身份验证添加到我的网站。

到目前为止,我已经添加了以下内容:


app.UseAuthentication();


services.AddAuthentication("MyCookieAuthenticationScheme")
    .AddCookie(options => {
    options.AccessDeniedPath = "/Account/Forbidden/";
    options.LoginPath = "/Account/Unauthorized/";
});

在我的登录代码中,我有
await HttpContext.SignInAsync("MyCookieAuthenticationScheme", principal);
principleClaimsPrincipal

当我登录到我的站点并致电上面的行时,出现错误:



我错过了什么?

最佳答案

您说过希望默认方案为“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# - InvalidOperationException : No IAuthenticationSignInHandler is configured to handle sign in for the scheme: MyCookieAuthenticationScheme,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45776374/

10-11 10:23