1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

I want to pass data from the UI (Angular) to API (ASP .NET Core)

Discussão em 'Angular' iniciado por Vengat Ramanan, Outubro 12, 2024.

  1. Username and password are passed to the API when I debug it, but from UI it doesn't.

    onSubmit() {
    console.log(this.loginform.value);
    var url = 'Auth/Login';
    this.Service.post(url,this.loginform.value).subscribe((user:any) => {
    if(user.status = true) {
    this.route.navigate(["/dashboard"]);
    }
    else {

    }
    });
    }


    This is the Api where I need to check the username and password.
    In swagger it works fine, but the data that I enter in the UI returns "unknown error".

    [HttpPost("Login")]
    public IActionResult Login([FromBody] User login)
    {
    if(login == null || String.IsNullOrEmpty(login.username) || String.IsNullOrEmpty(login.password))
    {
    return BadRequest("Email and Password is Required");
    }

    var user = AuthenticateLogin(login);

    if (user != null)
    {
    // Generate JWT token
    var token = GenerateToken(user);
    return Ok(new
    {
    message = "Logged In Successfully",
    token = token
    });
    }
    else
    {
    return Unauthorized(new { message = "Invalid Credentials" });
    }
    }

    private Login AuthenticateLogin([FromBody]User Logindetails)
    {
    var Loginuser = _context.Login.FirstOrDefault(a => a.Email == Logindetails.username);

    if(Loginuser != null)
    {
    if(Loginuser.password == Logindetails.password)
    {
    return new Login
    {
    Email = Loginuser.Email
    };
    }
    }

    return null;
    }

    Continue reading...

Compartilhe esta Página