[英]How to redirect to area after login asp.net core 3.0?
我创建了一个名为 Principal 的新区域,还在该区域中创建了一个名为 Principal 的 controller 和一个名为 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();
}
}
这是 controller
[Area("Principal")]
public class PrincipalController : Controller
{
public IActionResult Index()
{
return View();
}
}
您可以使用 RedirectToAction:
RedirectToAction("Your ActionName", "Your ControllerName");
在这种情况下:
RedirectToAction("Index", "PrincipalController ");
替换这一行:
return LocalRedirect(returnUrl);
和:
return RedirectToAction("Index", "Principal", new { area = "Principal" });
不要忘记添加区域路线,如图所示。 例如:
endpoints.MapControllers();
endpoints.MapAreaControllerRoute(
"Principal", "Principal",
"Principal/{controller=Principal}/{action=Index}/{id?}");
您还可以修改相关的登录方式。 例如,如果您的应用程序使用 2FA,则在OnPostAsync
的LoginWith2fa.cshtml.cs
function 中。
这对我有用我希望它对每个需要它的人都有用。
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" });
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.