我创建了一个名为 Principal 的新区域,我还在这个区域中创建了一个名为 Principal 的控制器和一个名为 index 的视图。我需要在用户登录后显示此视图。请帮助我。
public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
returnUrl = returnUrl ?? Url.Content("~/");
if (ModelState.IsValid)
{
// This doesn't count login failures towards account lockout
// To enable password failures to trigger account lockout, set lockoutOnFailure: true
var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: true);
if (result.Succeeded)
{
_logger.LogInformation("User logged in.");
return LocalRedirect(returnUrl);
}
if (result.RequiresTwoFactor)
{
return RedirectToPage("./LoginWith2fa", new { ReturnUrl = returnUrl, RememberMe = Input.RememberMe });
}
if (result.IsLockedOut)
{
_logger.LogWarning("User account locked out.");
return RedirectToPage("./Lockout");
}
else
{
var user = await _userManager.FindByNameAsync(Input.Email);
if (user == null)
{
ModelState.AddModelError(string.Empty, "Invalid UserName.");
}
else if (!await _userManager.CheckPasswordAsync(user, Input.Password))
{
ModelState.AddModelError(string.Empty, "Invalid Password.");
}
return Page();
}
}
这是控制器
[Area("Principal")]
public class PrincipalController : Controller
{
public IActionResult Index()
{
return View();
}
}
在此处输入图像描述
替换此行:
return LocalRedirect(returnUrl);
跟:
return RedirectToAction("Index", "Principal", new { area = "Principal" });
不要忘记添加区域路线,如下所示。例如:
endpoints.MapControllers();
endpoints.MapAreaControllerRoute(
"Principal", "Principal",
"Principal/{controller=Principal}/{action=Index}/{id?}");
您还可以修改相关的登录方式。例如,在OnPostAsync
LoginWith2fa.cshtml.cs
函数中,如果您的应用程序使用2FA
您可以使用 RedirectToAction:
RedirectToAction("Your ActionName", "Your ControllerName");
在这种情况下:
RedirectToAction("Index", "PrincipalController ");
这对我有用,我希望它对每个需要它的人都有效。
启动
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseEndpoints(endpoints =>
{
endpoints.MapAreaControllerRoute(
name: "myIdentity",
areaName: "Identity",
pattern: "Identity/{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
}
控制器
public IActionResult Index()
{
return RedirectToAction("Login","Account",new { area = "Identity" });
}