问题是此代码正在从整个表中获取用户名和密码,例如如果我输入第一人称用户名和第二人称密码,此代码仍在获取信息,但我只想获取一个具有相同用户名和密码的用户信息帮帮我。
[System.Web.Http.AcceptVerbs("GET","POST")]
[HttpGet]
public User Login(string username, string password)
{
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
return null;
var user = db.Users.SingleOrDefault(x => x.Email == username);
var pass = db.Users.SingleOrDefault(x => x.Password == password);
if (user == null || pass==null )
return null;
return user;
}
答案 0 :(得分:0)
您想要类似的东西
public User Login(string username, string password)
{
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
return null;
var user = db.Users.SingleOrDefault(x => x.Email == username);
var pass = user?.Password;
if (user == null || pass == null)
return null;
if (pass != password)
return null;
return user;
}