asp.net Core Authentication Login (Redirect to Admin)

Viewed 331

Login Authentication is success In AccountController but Redirect To Admin not working:

AccountController

[Authorize]
public class AccountController : Controller
{
    private readonly UserManager<AppUser> userManager;
    private readonly SignInManager<AppUser> signInManager;
    
    public AccountController(UserManager<AppUser>userManager,SignInManager<AppUser>signInManager)
    {
        this.userManager = userManager;
        this.signInManager = signInManager;
    }
    [AllowAnonymous]
    public IActionResult Login(string returnUrl)
    {
        ViewBag.returnUrl = returnUrl;
        return View();
    }
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Login(LoginModel details, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            AppUser user = await userManager.FindByEmailAsync(details.Email);
            if(user!=null)
            {
                await signInManager.SignOutAsync();
                //var tryCount = user.AccessFailedCount;
                //bool lockUser = tryCount >= 5;
                Microsoft.AspNetCore.Identity.SignInResult result = await signInManager.PasswordSignInAsync(
                    user, details.Password, false, false);
                if(result.Succeeded)
                {
                    return RedirectToAction("Index", "Admin");
                   // return Redirect(returnUrl);
                    //return Redirect(returnUrl);
                }
                ModelState.AddModelError(nameof(LoginModel.Email), "Invalid user or password");
            }                

        }
        return View(returnUrl);
    }
    public async Task<IActionResult> Logout()
    {
        await signInManager.SignOutAsync();
        return RedirectToAction("Index", "home");

    }
}

view account controller

Model LoginModel

@model LoginModel
<div class="bg-primary m-1 p-1 text-white"><h4>LogIN</h4></div>
<div class="text-danger"asp-validation-summary="All"></div>
<form asp-action="Login" method="post">
<input type="hidden" name="returnUrl" value="@ViewBag.returnUrl" />
<div class="form-group">
<label asp-for="Email"></label>
<input asp-for="Email" class="form-control" />
</div>
<div class="form-group">
<label asp-for="Password"></label>
<input asp-for="Password" class="form-control" />
</div>
<button class="btn btn-primary" type="submit">LogIn</button>
</form>

When we redirect to AdminController , redirect doesn't work correctly while user is exist and user and password are true.

1 Answers

I guess you've configured your routing correctly to automatically route to your method. In net core 3.1 for example you'll have something like this:

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");
});

If you still have issues maybe you need to provide some kind of parameter to route the request. Something like this:

return RedirectToAction("Index", "Admin", new { id = 1 });

And make sure your action method verb is GET. Redirections are not supported in HTTP with POST.

Related