locked
Second login form in one solution RRS feed

  • Question

  • User-865174439 posted

    I start use ASP.Net Identity in my app. I have one page (/identity/account/login) where i can type login and password and login to page. But i want create second login page in home page. In home page i have button who create pop-up where i would like create second login form. It is posible?

    First login form (deafult):

    Second login form (as pop-up in home page):

    What I have tried:

    In home page i try something like this, but it doesn't work

    <partial name="~/Areas/Identity/Pages/Account/Login.cshtml" model="@LoginModel" view-data="ViewData" />

    Whats more if click button in popup i create function:

    $("#login").click(function () {
            Login();
         
             });
    
    
              function Login() {
                  var InputLogin = $('#email').val();
                  var InputPassword = $('#password').val();
              
            $.ajax({
                type: "POST",
                url: 'Identity/Pages/Login/OnPostAsync',
                data: { "Input.Login": InputLogin, "Input.Email": InputLogin },
    
                dataType: "json",
                success: function () { alert('Ok'); },
                  
                error: function () { alert('Don't ok'); },
            });
            
            }

    Tuesday, May 12, 2020 7:31 PM

Answers

  • User-474980206 posted

    you have a couple issues with your code.

    1) your post data is not correct (you missed the password, remember me and the antiforgery token) :

    data = {
      Input.Email:email},
      Input.Password: password,
      Input.RememberMe': rememberMe, 
      __RequestVerificationToken: forgeryToken
    }  
    

    2) the response is html so remove the dataType.

    3) you will need parse the response html to see if successful. one workaround, would be to pass return url that returns a json true. then if the json response is true or, else failed. the works because on success the OnPostAsync redirects to the return url. you can have this url return ok.   

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 12, 2020 8:04 PM

All replies

  • User753101303 posted

    Hi,

    polunyt

    it doesn't work

    Instead always tell what actually happens. Use F12 networlk to see if the Ajax request happens or F12 Console for a possible JavaScript error. For now it seems it could be caused by having a single quote within single quotes ie 

    alert('Don't ok'); // try alert("Don't ok"); instead

    But as always when guessing from the code your first problem could be something else maybe not even related to what you shown.

    Edit: unrelated but the 2nd form seems to handle as well using a login/password. Isn't it enough to have this as a single login form rather than having a first form and then showing a popup one of the optino being apparently just the same ???

    Tuesday, May 12, 2020 7:50 PM
  • User-474980206 posted

    you have a couple issues with your code.

    1) your post data is not correct (you missed the password, remember me and the antiforgery token) :

    data = {
      Input.Email:email},
      Input.Password: password,
      Input.RememberMe': rememberMe, 
      __RequestVerificationToken: forgeryToken
    }  
    

    2) the response is html so remove the dataType.

    3) you will need parse the response html to see if successful. one workaround, would be to pass return url that returns a json true. then if the json response is true or, else failed. the works because on success the OnPostAsync redirects to the return url. you can have this url return ok.   

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 12, 2020 8:04 PM
  • User-865174439 posted

    Very thanks for help!

    I create something like this

    $.ajax({
                type: "POST",
                url: 'Home/Logowanie',
                data: {
                email:"adam@atma.com.pl",
                password: "3Ac2uq94#",
                first: false, 
                second: false,
                },
                
            
                success: function () { alert('Sukces'); },
                  
                error: function () { alert('Niesukces'); },
            });

    and in HomeController create function called Logowanie

    public async Task<IActionResult> Logowanie(string email, string password, bool first, bool second)
            {
                string returnUrl = null;
                returnUrl = returnUrl ?? Url.Content("~/");
                email = email.ToUpper();
              
                if (ModelState.IsValid)
                {
                  
                    var user = await _userManager.FindByEmailAsync(email);
                    var result = await _signInManager.CheckPasswordSignInAsync(user, password, first);
                    if (result.Succeeded)
                    {
                        _logger.LogInformation("User logged in.");
                        return LocalRedirect(returnUrl);
                    }
                    if (result.RequiresTwoFactor)
                    {
    
                    }
                    if (result.IsLockedOut)
                    {
                        _logger.LogWarning("User account locked out.");
                        return RedirectToPage("./Lockout");
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                       
                    }
                }
    
                // If we got this far, something failed, redisplay form
                return Page();
            }

    If i create breakpoint var user is email (is ok), var result is Succesed (is ok) and run first if. But The user is still not logged in to the website (if he is logged - @if (SignInManager.IsSignedIn(User)) , his username show in Index Page)

    Wednesday, May 13, 2020 8:05 AM
  • User-865174439 posted

    Ok, i fix it. I replace CheckPasswordSignInAsync toPasswordSignInAsync

    Wednesday, May 13, 2020 8:09 AM