我创建了用户登录页面。当我单击提交按钮时,一切正常。 我的意思是,当我单击“提交”按钮时,该方法成功了:
但是最终用户无法访问已定义的控制器。
这是我的身份ApplicationUser模型:
public class ApplicationUser : IdentityUser
{
public string UserProfile { get; set; }
}
这是我使用ApplicationUser
模型的Login视图模型:
public class LoginViewModel
{
[Required(ErrorMessage = "AN ERROR")]
public string UserName { get; set; }
[Required(ErrorMessage = "AN ERROR")]
[DataType(DataType.Password)]
public string Password { get; set; }
[Display(Name = "AN ERROR")]
public bool RememberMe { get; set; }
}
这是我的用户登录视图:
@model WOWapplication.Models.ViewModels.LoginViewModel
@{
ViewData["Title"] = "Login";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@addTagHelper * , Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper * , WarOfWords
<form asp-controller="Account" asp-action="Login" method="post" asp-route-returnUrl="@ViewData["ReturnUrl"]">
<input asp-for="UserName" type="text"/>
<span asp-validation-for="UserName"></span>
<input asp-for="Password" type="password"/>
<span asp-validation-for="Password"></span>
<div>
<input asp-for="RememberMe" type="checkbox" />
<label asp-for="RememberMe"></label>
</div>
<div>
<button type="submit">ورود</button>
<button asp-controller="Register" asp-action="Register">Login</button>
</div>
</form>
最后这是处理上述代码的控制器:
namespace WOWapplication.Controllers
{
public class AccountController : Controller
{
private readonly SignInManager<ApplicationUser> _SignInManager;
public AccountController(SignInManager<ApplicationUser> signInManager)
{
_SignInManager = signInManager;
}
[HttpGet]
public IActionResult Login(string returnUrl = null)
{
ViewData["ReturnUrl"] = returnUrl;
return View();
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
{
ViewData["ReturnUrl"] = returnUrl;
if (ModelState.IsValid)
{
var result = await _SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, lockoutOnFailure: false);
if (result.Succeeded)
{
return RedirectTolLocal(returnUrl);
}
else
{
ModelState.AddModelError(string.Empty, "Wrong User name or password");
return View(model);
}
}
return View(model);
}
private IActionResult RedirectTolLocal(string returnUrl)
{
if (Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction(nameof(HomeController.Index), "Home");
}
}
}
}
此外,我使用[Authorize(Roles = "Admin")]
来执行我希望管理员访问该控制器的操作。
请帮助我找到项目中的错误。因为此问题没有错误,并且一切都正确完成,但最终登录无法成功完成,并且Admin用户没有权限。请记住,控制器中的所有操作均成功。谢谢大家。
答案 0 :(得分:2)
我认为您的代码是正确的。跟随底部:
AspNetRoles
并确保表中有Rolse
AspNetUserRoles
并确保正确插入用户角色如果正确插入了Roles和UserRoles,请通过以下代码在Login
操作中获取用户的角色:
var userRole = await _userManager.GetRolesAsync(user).Result.ToArray();
OR
var roles = await _userManager.GetRolesAsync(user);
我在这里输入了完整的登录代码:
[HttpPost]
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
{
ViewData["ReturnUrl"] = returnUrl;
if (ModelState.IsValid)
{
var result = await _signInManager.PasswordSignInAsync(model.UserName,
model.Password, model.RememberMe, lockoutOnFailure: false);
if (result.Succeeded)
{
//Success Login
var user = await _userManager.FindByNameAsync(model.UserName);
var userRole = _userManager.GetRolesAsync(user);
}
}
答案 1 :(得分:-1)
我的问题已通过将app.UseAuthentication();
放在{.1}}方法中的startup.cs中解决了