How to solve the authentication issue?

61 Views Asked by At

I have two controllers: Login and My Account. When I enter my username and password on the login page and the login is successful, I want it to open the Index page in the My Account controller. However, it's redirecting to a page like https://localhost:7050/Account/Login?ReturnUrl=%2FMyAccounts and I'm getting a 404 error. Even though I've logged into the system and my email is confirmed, why is it giving me this error? Can you help me? I'm sending the code for the controllers. Thank you.

Login Controller

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

MyAccounts Controller

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

        }
        
    }
}
1

There are 1 best solutions below

0
Qiang Fu On

You redirect to this page because it didn't pass the [Authorize]. After authorize failed, it automatically redirect to a default login URL. You could configure it like below to redirect to your customize login page.

        builder.Services.AddIdentity<IdentityUser, IdentityRole>(
            ...
            ).AddEntityFrameworkStores<ApplicationContext>();

        builder.Services.ConfigureApplicationCookie(options =>
        {
           options.LoginPath = new PathString("/Login/Index");
        });

For a general reason of this authorize failure, make sure you use

app.UseAuthentication();
app.UseAuthorization();