in asp.net core web api
i have a POST
method that get username and password in FormUrlEncodedContent
format from client. But the "entry" parameter is null
.
how can i access the username and password in Login method in web api ?
here is my code:
client:
public async Task<string> login2(string command , string username , string password)
{
string exist = string.Empty;
FormUrlEncodedContent dataForm = new FormUrlEncodedContent(new[] {
new KeyValuePair<string,string>("username",username),
new KeyValuePair<string, string>("password",password)
});
var resp = await http.PostAsync(command,dataForm);
exist = await resp.Content.ReadAsStringAsync();
return exist;
}
server :
public IActionResult Login([FromForm] string entry)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
Console.WriteLine(entry);
return Ok(entry);
}
Create a model to hold the posted information
public class LoginModel {
public string username { get; set; }
public string password { get; set; }
}
and update the action to expect that from the from
public IActionResult Login([FromForm] LoginModel entry) {
if (!ModelState.IsValid) {
return BadRequest(ModelState);
}
// access the username and password
var username = entry.username;
var password = entry.password;
return Ok();
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments