我有两个控制器:登录和我的帐户。当我在登录页面输入用户名和密码并登录成功时,我希望它打开“我的帐户”控制器中的“索引”页面。但是,它重定向到类似 https://localhost:7050/Account/Login?ReturnUrl=%2FMyAccounts 的页面,并且我收到 404 错误。尽管我已经登录系统并且我的电子邮件已确认,但为什么它给我这个错误?你能帮助我吗?我正在发送控制器的代码。谢谢你。
登录控制器
using EasyCashIdentityProject.EntityLayer.Concrete;
using EasyCashIdentityProject.PresentationLayer.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
namespace EasyCashIdentityProject.PresentationLayer.Controllers
{
public class LoginController : Controller
{
private readonly SignInManager<AppUser> _signInManager;
private readonly UserManager<AppUser> _userManager;
public LoginController(SignInManager<AppUser> signInManager, UserManager<AppUser> userManager)
{
_signInManager = signInManager;
_userManager = userManager;
}
[HttpGet]
public IActionResult Index()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Index(LoginViewModel loginViewModel)
{
var result = await _signInManager.PasswordSignInAsync(loginViewModel.Username, loginViewModel.Password, false, true);
if (result.Succeeded)
{
var user = await _userManager.FindByNameAsync(loginViewModel.Username);
if (user.EmailConfirmed == true) {
return RedirectToAction("Index", "MyAccounts");
}
}
return View();
}
}}
我的帐户控制器
using EasyCashIdentityProject.DtoLayer.Dtos.AppUserDtos;
using EasyCashIdentityProject.EntityLayer.Concrete;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace EasyCashIdentityProject.PresentationLayer.Controllers
{
[Authorize]
public class MyAccountsController : Controller
{
private readonly UserManager<AppUser> _userManager;
public MyAccountsController(UserManager<AppUser> userManager)
{
_userManager = userManager;
}
[HttpGet]
public async Task<IActionResult> Index()
{
var values = await _userManager.FindByNameAsync(User.Identity.Name);
AppUserEditDto appUserEditDto = new AppUserEditDto();
appUserEditDto.Name= values.Name;
appUserEditDto.Surname= values.Surname;
appUserEditDto.PhoneNumber= values.PhoneNumber;
appUserEditDto.Email= values.Email;
appUserEditDto.City = values.City;
appUserEditDto.District = values.District;
appUserEditDto.ImageUrl = values.ImageUrl;
return View(appUserEditDto);
}
[HttpPost]
public async Task<IActionResult> Index(AppUserEditDto appUserEditDto)
{
if(appUserEditDto.Password==appUserEditDto.ConfirmPassword)
{
var user = await _userManager.FindByNameAsync(User.Identity.Name);
user.PhoneNumber = appUserEditDto.PhoneNumber;
user.Surname = appUserEditDto.Surname;
user.City = appUserEditDto.City;
user.District = appUserEditDto.District;
user.ImageUrl = "test";
user.Email = appUserEditDto.Email;
user.PasswordHash = _userManager.PasswordHasher.HashPassword(user, appUserEditDto.Password);
var result = await _userManager.UpdateAsync(user);
if(result.Succeeded)
{
return RedirectToAction("Index", "Login");
}
}
return View();
}
}
}
您重定向到此页面,因为它没有通过
[Authorize]
。授权失败后,自动重定向到默认登录URL。您可以像下面这样配置它以重定向到您的自定义登录页面。
builder.Services.AddIdentity<IdentityUser, IdentityRole>(
...
).AddEntityFrameworkStores<ApplicationContext>();
builder.Services.ConfigureApplicationCookie(options =>
{
options.LoginPath = new PathString("/Login/Index");
});
对于此授权失败的一般原因,请确保您使用
app.UseAuthentication();
app.UseAuthorization();