这是我的代码:这是提琴手使用的函数:
http://localhost:3334/Service/Login/?json={'username':'cara','password':'password'}
public ActionResult Login(JObject JSON)
{
var response = JsonResponse.OKResponse();
var username = JSON["username"].ToString();
var password = JSON["password"].ToString();
var helper = new MemberHelper();
//goes into here and never returns
if (helper.ValidateUser(username, password))
{
MongoCollection<User> users = db.GetCollection<User>();
var usr = users.FindAll().FirstOrDefault(u => u.UserName.Equals(username));
response.data.Add(usr);
}
else
{
return Json(JsonResponse.ErrorResponse("Invalid username or password provided!"), JsonRequestBehavior.AllowGet);
}
return Json(response, JsonRequestBehavior.AllowGet);
}
MemberHelper 中的 validateUser 方法:
public override bool ValidateUser(string username, string password)
{
var hash = Encoding.ASCII.GetBytes(password);
var provider = new SHA256CryptoServiceProvider();
for (int i = 0; i < 1024; i++) // 1024 round SHA256 is decent
hash = provider.ComputeHash(hash);
var pass = Convert.ToBase64String(hash);
MongoCollection<User> users = db.GetCollection<User>();
//***The following statement is where the program just stops***
var usr = users.FindAll().FirstOrDefault(u => u.UserName.Equals(username) && u.Password.Equals(pass));
...
}
和getCollection....
public MongoCollection<T> GetCollection<T>(string name = null)
{
string collectionName = name;
if (collectionName == null) {
collectionName = typeof(T).Name;
}
return Database.GetCollection<T>(collectionName);
}
我真的不知道出了什么问题。我是 linq 的新手,所以我不确定我是否违反了一些黄金法则。请帮忙!让我知道是否还有其他需要添加的内容。