ASP.NET Core 8 Web API returns status code 400 for .NET 8 client

This is driving me nuts and I can’t figure out what the problem is. I have one project consisting of Web APIs and another project, the client, which calls them. Both projects are built using VS 2022, .NET 8.

This is my authentication function:

    [AllowAnonymous]
    [HttpPost("Authenticate")]
    public async Task<UserLoginResponse> Authenticate(string lr)
    {
        UserLoginResponse resp = new UserLoginResponse { Success = false, ResultMessage = string.Empty, UserToken = "" };
        UserSignIn usi = default;

        if (string.IsNullOrEmpty(lr))
        {
            resp.Success = false;
            resp.ResultCodes = LoginResponseCodes.UserNamePasswordRequired;
            resp.UserToken = string.Empty;
            return resp;
        }

        try
        {
            usi = JsonConvert.DeserializeObject<UserSignIn>(lr);
        }
        catch (Exception ex) 
        {
            resp.Success = false;
            resp.ResultCodes = LoginResponseCodes.UserNamePasswordRequired;
            resp.UserToken = string.Empty;
            return resp;
        }

        if (string.IsNullOrEmpty(usi.Email) || string.IsNullOrEmpty(usi.Password))
        {
            resp.Success = false;
            resp.ResultMessage = "Valid User Name (email)/Password Required";
        }
        else
        {
            //resp = await _userService.Authenticate(usi);
            //if(resp == null)
            //{
            //    resp = new UserLoginResponse { Success = false, ResultMessage = "NULL Error", UserToken = "" };
            //}
        }

        return resp;
    }

And here is one of the many client variations I’ve tried, which all result in a

StatusCode: 400, ReasonPhrase: ‘Bad Request’

Code:

    [AllowAnonymous]
    [HttpPost("Authenticate")]
    public async Task<UserLoginResponse> Authenticate(string lr)
    {
        UserLoginResponse resp = new UserLoginResponse { Success = false, ResultMessage = string.Empty, UserToken = "" };
        UserSignIn usi = default;

        if (string.IsNullOrEmpty(lr))
        {
            resp.Success = false;
            resp.ResultCodes = LoginResponseCodes.UserNamePasswordRequired;
            resp.UserToken = string.Empty;
            return resp;
        }

        try
        {
            usi = JsonConvert.DeserializeObject<UserSignIn>(lr);
        }
        catch (Exception ex) 
        {
            resp.Success = false;
            resp.ResultCodes = LoginResponseCodes.UserNamePasswordRequired;
            resp.UserToken = string.Empty;
            return resp;
        }

        if (string.IsNullOrEmpty(usi.Email) || string.IsNullOrEmpty(usi.Password))
        {
            resp.Success = false;
            resp.ResultMessage = "Valid User Name (email)/Password Required";
        }
        else
        {
            //resp = await _userService.Authenticate(usi);
            //if(resp == null)
            //{
            //    resp = new UserLoginResponse { Success = false, ResultMessage = "NULL Error", UserToken = "" };
            //}
        }

        return resp;
    }

There are no errors produced anywhere, at least none I’ve been able to find.

If I test my API call using the Swagger client, it works fine. It also works fine when I use the following Javascript in an html page.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
    function CallWebApi() {
        var url = "https://localhost:7598/api/BlueUsers/Authenticate?lr=%7B%22Email%22%3A%22testemail%40gmail.com%22%2C%22Password%22%3A%22hjgtdsrsdes%22%7D";
        $.ajax({
            url: url,
            type: "POST",
            success: function (data) {
                console.log(data);
            },
            error: function (xhr, status, error) {
                console.log(xhr.responseText);
            }
        });
    }
    CallWebApi();

Could someone please help me with what I’m missing and/or doing incorrectly in my .NET client?

Thank you

4

Your question is missing the actual .NET 8 client-side code so it’s rather difficult to be more specific but here’s a few pointers which come to mind:

  1. Increasing the logging level through an appsettings.json file will allow you to see what the various converters, middleware and controllers are doing, and most importantly it allows you to compare the differences between the call from the AJAX snippet vs. your .NET 8 client.
  2. Like pointed out in the comments, the default binding logic of an ASP.NET Core application will try to bind lr from the body of the request. You are using a query parameter, so adding a [FromQuery] parameter attribute helps the framework determine that the parameter is expected to come from the query part of the request.
  3. In the .NET 8 client code, be sure to check that the query string is properly encoded.

Caveat: If I had 50 rep I would’ve posted a comment instead of an answer. If there’s a moderator around feel free to move this into a comment instead.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật