ASP.NET Core 2没有配置身份验证处理程序来处理该方案

al.*_*val 2 c# asp.net authorization asp.net-core

我想在ASP.NET Core 2应用程序中提供授权.在账户/登录中发送带有数据的模型后,在调用之后await Authenticate(user),我收到一条错误消息.我无法理解哪里缺乏描述.

Startup.cs

//ConfigureServices
services.AddAuthentication(options =>
{
    options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
}).AddCookie("TmiginScheme", options =>
{
    options.LoginPath = "/Account/Login";
    options.LogoutPath = "/Account/Logout";
    options.ExpireTimeSpan = TimeSpan.FromHours(1);
    options.SlidingExpiration = true;
});

//Configure
app.UseAuthentication();
Run Code Online (Sandbox Code Playgroud)

的AccountController

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginModel model)
{
    if (ModelState.IsValid)
    {
        User user = null;
        Cryptex cryptex = new Cryptex();
        string password = cryptex.EncryptText(model.Password, "TMigin");

        // ???? user
        user = fStorage.Users.GetUserByLogin(model.Login);
        if (user != null)
        {
            if (string.Compare(user.Password, password) != 0)
            {
                user = null;
            }
        }

        if (user != null)
        {
            await Authenticate(user);
            return RedirectToAction("Index", "CMS");
        }

        else
        {
            // ???????? ?????? ?????
            ModelState.AddModelError("", "?????? ?????");
        }
    }

    return View(model);
}

private async Task Authenticate(User user)
{
    var claims = new List<Claim>
    {
        new Claim(ClaimsIdentity.DefaultNameClaimType, user.Name),
        new Claim("CMS", "True")
    };
    var identity = new ClaimsIdentity(claims);
    var principal = new ClaimsPrincipal(identity);
    await HttpContext.Authentication.SignInAsync("TmiginScheme", principal);
}
Run Code Online (Sandbox Code Playgroud)

固定

不行,因为我把代码放在后面app.UseMvc(...){}.在截图中的正确位置.

正确地

小智 6

我有同样的问题,但作者的解决方案对我不起作用.我正在从.NET Core 1.1迁移到.NET Core 2.0.

就我而言,我正在使用:

await HttpContext.Authentication.SignInAsync(...);
await HttpContext.Authentication.SignOutAsync(...);
Run Code Online (Sandbox Code Playgroud)

我应该使用:

await HttpContext.SignInAsync(...);
await HttpContext.SignOutAsync(...);
Run Code Online (Sandbox Code Playgroud)

  • 你是一个救星。我已经尝试了 8 个多小时,结果证明这是解决方案。 (2认同)

Ali*_*Ali 3

我想问题是您正在将默认方案配置为Cookies您使用时的默认options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;方案,而不是TmiginScheme您使用时使用的不同方案AddCookie("TmiginScheme"

比在AccountController您创建新的ClaimsIdentity未指定身份验证类型的情况下,最后您尝试使用与您在 中指定的方案名称不同的方案名称登录options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;

要解决您的问题,请更改AddCookie("TmiginScheme".AddCookie(CookieAuthenticationDefaults.AuthenticationScheme.

改成。var identity = new ClaimsIdentity(claims);var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);

最后await HttpContext.Authentication.SignInAsync("TmiginScheme", principal);改为await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);