我就應該總是返回相同的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 –