locked
Unable to redirect from login page RRS feed

  • Question

  • User-578960655 posted

    I'm trying to get through to Login process but after i input the data (which data that stated in Database) it does not want to redirect to the next page. The thing is that i don't receive any error message or warning signal. 

    Login.cshtml.cs

            private readonly SignInManager<LoginViewModel> signInManager;
    
            public LoginModel(SignInManager<LoginViewModel> signInManager)
            {
                this.signInManager = signInManager;
            }
            [HttpGet]
            public IActionResult Login()
            {
                return Page();
            }
    
            [HttpPost]
            public async Task<IActionResult> Login(LoginViewModel model)
            {
                if (ModelState.IsValid)
                {
                    var result = await signInManager.PasswordSignInAsync(model.Email, model.Password, false, false);
    
                    if (result.Succeeded)
                    {
                        return RedirectToAction("index", "home");
                    }
    
                    ModelState.AddModelError(string.Empty, "Invalid Login attempt");
                 }
    
                return Page();
    
            }

    LoginViewModel

        public class LoginViewModel : IdentityUser
        {
            [Required]
            [Display(Name = "Email")]
            [DataType(DataType.EmailAddress)]
            public override string Email { get; set; }
    
            [Required]
            [Display(Name = "Password")]
            [DataType(DataType.Password)]
            public string Password { get; set; }
        }

    Edit: i removed unrelated code

    Monday, May 4, 2020 4:17 PM

All replies

  • User475983607 posted

    The ViewModel is not a ViewModel it's your user identity.   Your design is a bit confusing.   Can't you just create a standard view model?  

        public class LoginViewModel
        {
            [Required]
            [Display(Name = "Email")]
            [DataType(DataType.EmailAddress)]
            public string Email { get; set; }
    
            [Required]
            [Display(Name = "Password")]
            [DataType(DataType.Password)]
            public string Password { get; set; }
        }
    

    Then use the default IdentityUser?

    private readonly SignInManager<IdentityUser> _signInManager;

    Monday, May 4, 2020 6:41 PM
  • User-578960655 posted

    Hai mgebhard, i'm sorry for your confusion, i'm still learning new APIs in Visual Studio. Which part is it that make you confused? i would happily share new information.

    Anyway, i have used the default IdentityUser before but it does not want to redirect. That is why i used additional identity for login called LoginViewModel as IdentityUser reference.

    Tuesday, May 5, 2020 2:52 AM
  • User-821857111 posted

    You should try debugging the Login action and step through the code to see what happens.

    https://docs.microsoft.com/en-us/visualstudio/debugger/debugger-feature-tour?view=vs-2019

    Tuesday, May 5, 2020 8:56 AM
  • User-1955300613 posted

    Hi emirryhn,

    Thanks for your question.

    You can try the following methods to solve your problems. It's on the basis of my code, may be they can give you some inspiration.

    1,the 'return Page()' in your [HttpPost] may be replaced with 'return Page(model)'?

    2,May be your html file has some error ,you can try to use tag helper to do the 'jump' job, it's easy to use:

    https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/authoring?view=aspnetcore-3.1

     

    It will be better to show your whole code, so I can help you reproduce the code and find the exact error.

    Best regards,

    Tsaisoul

    Friday, May 15, 2020 2:05 AM
  • User-2054057000 posted

    I saw the code you provided here. It seems you are implementing the Login Procedure with Identity Membership System. Kindly refer this tutorial which deals with Authentication of Users in Identity in ASP.NET Core

    Tuesday, June 2, 2020 3:15 PM