1

I can't produce my code despite the different questions and answers found on stackoverflow.

Here is my problem, I want to receive the Username and Password of the client to perform the verification (the equality is only for the example).

However the LoginPost variable is all the time null. Furthermore I have difficulty understanding the best way to send the client an http code and json at the same time.

using Microsoft.AspNetCore.Mvc;
using web.Models;

namespace web.Controllers
{
    [Produces("application/json")]
    [Route("api/[controller]")]
    public class LoginController : ControllerBase
    {
        [HttpPost]
        public ActionResult<LoginPost> LoginPost([FromBody] LoginPost loginPost)
        {
            // (1) FAIL, loginPost variable is null

            if (loginPost.Username == loginPost.Password)
            {
                return loginPost;
            }


            // (2) How to add a message like "User/Password fails"

            return Unauthorized(); 
        }

    }
}

Note the annotations (1) and (2) in the code.

Here's the jQuery code

$.ajax({
        url: '../api/Login',
        type: 'POST',
        dataType: "json",
        contentType: "application/json, charset=utf-8",
        data: {
            Username: username,
            Password: password
        },
        statusCode: {
            200: function (data) {
                console.log(data);
            }
            401: function (data) {
                console.log(data);
            }
            500: function (data) {
                console.log(data);
            }
        }
});

and LoginPost.cs class:

public class LoginPost
{
    public string Username { get; set; }
    public string Password { get; set; }
}
3
  • So what does the above code do? Is there an error? If so, what is it? Commented Oct 20, 2018 at 11:12
  • @DavidG I explained it, I don't understand the interest of your message Commented Oct 20, 2018 at 11:15
  • @DavidG I may repeat myself but the [FromBody] LoginPost data is all the time null. I can't receive data by POST Commented Oct 20, 2018 at 12:05

3 Answers 3

3

Since you don't seem to want to use json, post the data as a form.

Remove contentType: "application/json, charset=utf-8", from your ajax call.

'application/x-www-form-urlencoded; charset=UTF-8' is the default so the data will be sent as a form. Also remove [FromBody] Attribute and it should work

Sign up to request clarification or add additional context in comments.

Comments

1

The login controller is working fine, I tested it with Postman. The problem lies with the ajax call: it does not send the data as the body of the POST request but as URL query parameters (i.e. Username=username&Password=password).

To send the data in the body of the POST request you need to send the data as json string:

data: JSON.stringify({
    "Username": "username",
    "Password": "password"
}),

2 Comments

Well! But how to avoid the JSON.stringify function? I need to change [FromBody] to another ? With the old .NET version, it use [FromUri] but it doesn't exist here
The simplest solution would be to remove the [FromBody] attribute and remove contentType from the ajax call. Then the data will be sent as form data and it just works.
1

For (2) question, you can return status code :

return StatusCode(401, "User/Password fails");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.