没有指定authenticationScheme,也没有找到DefaultChallengeScheme Cookies身份验证 [英] No authenticationScheme was specified, and there was no DefaultChallengeScheme found Cookies Authentication
问题描述
我正在使用以下代码在使用cookie的ASP.NET Core 2.0中进行身份验证
I am using the below code for authentication in ASP.NET Core 2.0 using cookies
services
.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie("MyCookieMiddlewareInstance", options =>
{
options.AccessDeniedPath = new PathString("/Account/Login");
options.LoginPath = new PathString("/Account/Login");
options.LogoutPath = new PathString("/Account/LogOff");
});
我遇到错误:
未指定authenticationScheme,也未找到DefaultChallengeScheme
No authenticationScheme was specified, and there was no DefaultChallengeScheme found
Cookie设置如下:
The cookies setup is below:
var claims = new List<Claim>
{
new Claim(ClaimTypes.NameIdentifier, userId.ToString()),
new Claim(ClaimTypes.Name, userName)
};
var identity = new ClaimsIdentity(claims, "Forms");
identity.AddClaim(new Claim(ClaimTypes.Role, "ADMIN"));
var principal = new ClaimsPrincipal(identity);
HttpContext.Authentication.SignInAsync("MyCookieMiddlewareInstance", principal, new AuthenticationProperties
{
IsPersistent = isPersistent,
ExpiresUtc = DateTime.UtcNow.AddYears(1)
});
我做了一些研究,但没有找到解决方案.这是指向文档的链接我用过:
I did some research and didn't find the solution. Here is a link to the doc I used:
任何人都可以让我知道如何解决此问题吗?
Can anyone please let me know how can I resolve this issue?
推荐答案
authenticationBuilder.AddCookie("MyCookieMiddlewareInstance", …)
这将使用身份验证方案名称"MyCookieMiddlewareInstance"
注册一个cookie身份验证处理程序.因此,无论何时使用Cookie身份验证方案,都需要使用该确切名称,否则将找不到该方案.
This registers a cookie authentication handler using the authentication scheme name "MyCookieMiddlewareInstance"
. So whenever you are referring to the cookie authentication scheme, you will need to use that exact name, otherwise you will not find the scheme.
但是,在AddAuthentication
调用中,您使用的是不同的方案名称:
However, in the AddAuthentication
call, you are using a different scheme name:
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
这会将具有恒定值"Cookies"
的CookieAuthenticationDefaults.AuthenticationScheme
注册为默认身份验证方案.但是,从未注册具有该名称的方案!相反,只有"MyCookieMiddlewareInstance"
.
This registers the CookieAuthenticationDefaults.AuthenticationScheme
, which has the constant value "Cookies"
, as the default authentication scheme. But a scheme with that name is never registered! Instead, there’s only a "MyCookieMiddlewareInstance"
.
因此解决方案是为两个调用简单地使用相同的名称.您也可以只使用默认值,并删除显式名称.如果您没有多个方案并且需要更多控制权,那么实际上就不需要显式设置其名称.
So the solution is to simply use the same name for both calls. You can also just use the defaults, and remove the explicit names; if you don’t have multiple schemes and need more control, there isn’t really a need to explicitly set their names.
这篇关于没有指定authenticationScheme,也没有找到DefaultChallengeScheme Cookies身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!