Je souhaite fournir une autorisation dans l'application ASP.NET Core 2. Après l'envoi du modèle avec les données dans Account/Login, après l'appel de "await Authentifier (utilisateur) "Je reçois un message d'erreur. Je ne comprends pas où il y a un manque de description.AS.NET Core 2 Aucun gestionnaire d'authentification n'est configuré pour gérer le schéma
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);
}
fixe
ne fonctionne pas, comme je l'ai placé le code après la app.UseMvc (...) {}. Dans la capture d'écran, l'emplacement correct.
Vous êtes une bouée de sauvetage. J'ai essayé des trucs pour un solide 8 heures et plus, et cela s'est avéré être la solution. –