Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core 2 No authentication handler is configured to handle the scheme

I want to provide authorization in an ASP.NET Core 2 application. After sending the model with the data in Account/Login, after the call to await Authenticate(user), I get an error message. I can not understand where there is a lack of description.

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();

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);
}

fixed

Not working, as I placed the code after the app.UseMvc(...){}. In the screenshot the correct location.

correctly

like image 663
al.koval Avatar asked Dec 11 '22 10:12

al.koval


2 Answers

I had the same problem but author's solution didn't work for me. I was migrating from .NET Core 1.1 to .NET Core 2.0.

In my case I was using:

await HttpContext.Authentication.SignInAsync(...);
await HttpContext.Authentication.SignOutAsync(...);

and i should be using:

await HttpContext.SignInAsync(...);
await HttpContext.SignOutAsync(...);
like image 137
SideProject Avatar answered Jan 02 '23 05:01

SideProject


I guess the issue is you are configuring the Default Scheme to be Cookies when you used options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme; and than you used different Scheme which is TmiginScheme when you used AddCookie("TmiginScheme".

Than in AccountController you created new ClaimsIdentity without specifying the authentication type and finally you tried to Sign In using scheme name different than what you specified in options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;.

To solve your issue change AddCookie("TmiginScheme" to .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme.

Change var identity = new ClaimsIdentity(claims); to var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);.

Finally change await HttpContext.Authentication.SignInAsync("TmiginScheme", principal); to await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);

like image 37
Ali Avatar answered Jan 02 '23 03:01

Ali