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.
Below 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;
}
Below, is one of the many client variations I’ve tried, which all result in a StatusCode: 400, ReasonPhrase: ‘Bad Request’:
[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, within 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