【发布时间】:2020-10-22 15:43:04
【问题描述】:
Web API 代码:
// POST api/<MAUserController>
[HttpPost("AuthenticateUser")]
public async Task<ActionResult<MAUser>> PostAsync([FromHeader] string Email, [FromHeader] string Password)
{
string connString = configuration.GetConnectionString("DefaultConnection");
MAUserVM user = new MAUserVM();
user = await user.AuthenticateUserAsync(Email, Password, connString);
if (user.AuthenticationCode == 0)
{
return Ok(user._MAUser);
}
else if (user.AuthenticationCode == 100)
{
return NotFound("Email not found");
}
else if (user.AuthenticationCode == 200)
{
return NotFound("Incorrect password");
}
else
{
return NotFound();
}
}
客户代码:
using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokenString);
httpClient.DefaultRequestHeaders.Add("Email", Email);
httpClient.DefaultRequestHeaders.Add("Password", Password);
using (var response = await httpClient.PostAsync(API_URL, stringContent))
{
if (response.IsSuccessStatusCode)
{
string apiResponse = await response.Content.ReadAsStringAsync();
user = JsonConvert.DeserializeObject<MAUser>(apiResponse);
}
else
{
var str = response.StatusCode;
}
}
}
return user;
我只在 var str 中找到 Not Found,但从未找到相关内容 - “找不到电子邮件”或“密码错误”
【问题讨论】:
-
您没有阅读响应的内容。
标签: c# asp.net asp.net-web-api httpclient asp.net-core-3.1