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:
- 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.
- 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. - 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