我就应该总是返回相同的JSON解析的对象或不的WebAPI返回类型
例子:
在网站上,用户输入自己的用户名和密码。
当用户按提交他发送请求/api/logintest/{key}
。如果用户和密码匹配,该URL所调用的方法是否会返回像{errMsg:null}这样的JSON对象,如果不匹配,则返回{errMsg:“bad username or password”};或者如果用户的详细信息不匹配,我应该返回并显示错误消息,如果找到用户,则返回JSON中的用户对象?
这是我的代码
[ResponseType(typeof(User))]
[HttpPost]
[Route("api/logintest/{key}")]
public IHttpActionResult LoginTest(LoginForm luser , String key)
{
//test the api key
if(key == "jordanisthebest")
{
//we try to get the user by his username and password
User userReturn = UsersManager.getUserByEmailAndPassword(new User { Email = luser.Email, Password = luser.Password });
//if the user is null then we return the err message
if (userReturn == null)
ModelState.AddModelError("Email", "Bad email or password");
//if model state is not good we send err msg
if (!ModelState.IsValid)
return Ok(ModelState.getListErrorAndKey(null, typeof(LoginForm)));
//if all good we return the user
return Ok(userReturn);
}
return NotFound();
}
你看到了什么错误? – Prescott
你的问题不清楚。你不确定要返回什么吗? – Vinoth
远离这个问题,您不应该使用这种方式来验证用户身份,您应该使用由身份提供者(例如ADFS或Identity Server)发出的令牌来验证您的API –