I working on Login
in AdminController
(in the Admin
area).
Here is my AccountController
:
using Sneaker.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
namespace Sneaker.Areas.Admin.Controllers
{
[Area("Admin")]
public class AdminController : Controller
{
private UserManager<AppUserModel> _userManager;
private SignInManager<AppUserModel> _signInManager;
public AdminController(UserManager<AppUserModel> userManager, SignInManager<AppUserModel> signInManager)
{
_userManager = userManager;
_signInManager = signInManager;
}
[HttpPost]
public async Task<IActionResult> Login(UserModel user)
{
Microsoft.AspNetCore.Identity.SignInResult result = await _signInManager.PasswordSignInAsync(user.UserName, user.Password, false, false);
if (result.Succeeded)
{
return RedirectToAction("Index", "Admin");
}
ModelState.AddModelError("", "Error");
return RedirectToAction("Index", "Admin");
}
public IActionResult Index()
{
return View();
}
}
}
And the view for the Login
action :
@model UserModel
<!DOCTYPE html>
<!---Coding By CoderGirl | www.codinglabweb.com--->
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Login & Registration Form | CoderGirl</title>
<!---Custom CSS File--->
<link rel="stylesheet" href="~/css/style.css">
</head>
<body>
<div class="container">
<input type="checkbox" id="check">
<div class="login form">
<header>Login</header>
<form asp-action="Login" asp-controller="Admin">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="UserName" class="control-label"></label>
<input asp-for="UserName" class="form-control" />
<span asp-validation-for="UserName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Password" class="control-label"></label>
<input asp-for="Password" type="password" class="form-control" />
<span asp-validation-for="Password" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Login" class="btn btn-primary" />
</div>
</form>
</div>
</div>
</body>
</html>
I don't know why the index action works correctly, but when I try to open the login form, the page show could not be found
[Here my route folder View][1][1]: https://i.sstatic.net/nHZ5D.png
And the index work so well but the login action cant be access (outside the Admin area the login action work normal)
Here my map controller route
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "areas",
pattern: "{area:exists}/{controller=Admin}/{action=Index}/{id?}"
);
Can you guys know the problem? Many thanks
there is just post method for Login action in Admin Controller,
so... how u want see login form my friend? u need this :
[HttpGet]
public IActionResult Login()
{
return View();
}
after that u can see login page but but i think your login.cshtml have some problem too
<form asp-action="" asp-area="" asp-controller="" method="post"></form>