简体   繁体   中英

How to redirect to area after login asp.net core 3.0?

I have created a new area called Principal, also I have created in this area a controller called Principal and a view called index. I need to show this view afer user login. Please help me.

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();
        }
    }

This is controller

[Area("Principal")]
public class PrincipalController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
}

enter image description here

You can use RedirectToAction:

RedirectToAction("Your ActionName", "Your ControllerName");

In this case:

RedirectToAction("Index", "PrincipalController ");

Replace this line:

return LocalRedirect(returnUrl);

With:

return RedirectToAction("Index", "Principal", new { area = "Principal" });

Don't forget to add the area route as shown here . For example:

endpoints.MapControllers();
endpoints.MapAreaControllerRoute(
    "Principal", "Principal",
    "Principal/{controller=Principal}/{action=Index}/{id?}");

You can also modify related login methods. For example, in OnPostAsync function of LoginWith2fa.cshtml.cs if your application uses 2FA.

This works for me I hope it work for everyone who need it.

Startup

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();
       });
  }

Controller

  public IActionResult Index()
  {
      return RedirectToAction("Login","Account",new { area = "Identity" });
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM