Ich möchte die Autorisierung in der Anwendung ASP.NET Core 2 bereitstellen. Nach dem Senden des Modells mit den Daten in Konto/Login nach dem Aufruf von "abwarten Authentifizieren (Benutzer) "Ich erhalte eine Fehlermeldung. Ich kann nicht verstehen, wo es Mangel an Beschreibung gibt.AS.NET Core 2 Es ist kein Authentifizierungshandler für das Schema konfiguriert.
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();
Account
[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);
}
Fest
nicht berufstätig, wie ich den Code nach dem app.UseMvc platziert (...) {}. Im Screenshot der korrekte Standort.
Sie sind ein Lebensretter. Ich habe Sachen für eine solide 8+ Stunden probiert, und dies stellte sich als die Lösung heraus. –