C# 我的linq语句或GetCollection有问题<;T>;()
这是我的密码: 这是fiddler使用以下命令调用的函数:C# 我的linq语句或GetCollection有问题<;T>;(),c#,asp.net-mvc-3,linq,mongodb-.net-driver,C#,Asp.net Mvc 3,Linq,Mongodb .net Driver,这是我的密码: 这是fiddler使用以下命令调用的函数: http://localhost:3334/Service/Login/?json={'username':'cara','password':'password'} public ActionResult Login(JObject JSON) { var response = JsonResponse.OKResponse(); var username = JSON["username"].ToString();
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);
}
http://localhost:3334/Service/Login/?json={'username':'cara','password':'password'}
公共操作结果登录(JObject JSON)
{
var response=JsonResponse.OKResponse();
var username=JSON[“username”].ToString();
var password=JSON[“password”].ToString();
var helper=newmemberhelper();
//进了这里就再也不回来了
if(helper.ValidateUser(用户名、密码))
{
MongoCollection用户=db.GetCollection();
var usr=users.FindAll().FirstOrDefault(u=>u.UserName.Equals(UserName));
响应.数据.添加(usr);
}
其他的
{
返回Json(JsonResponse.ErrorResponse(“提供的用户名或密码无效!”),JsonRequestBehavior.AllowGet);
}
返回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));
...
}
public override bool ValidateUser(字符串用户名、字符串密码)
{
var hash=Encoding.ASCII.GetBytes(密码);
var provider=新的SHA256CryptoServiceProvider();
对于(int i=0;i<1024;i++)//1024轮SHA256是合适的
哈希=provider.ComputeHash(哈希);
var pass=Convert.ToBase64String(散列);
MongoCollection用户=db.GetCollection();
//***以下语句是程序停止的地方***
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);
}
public MongoCollection GetCollection(字符串名称=null)
{
字符串collectionName=name;
如果(collectionName==null){
collectionName=typeof(T).Name;
}
返回Database.GetCollection(collectionName);
}
我真的不知道出了什么问题。我是linq的新手,所以我不确定我是否打破了一些金科玉律。请帮忙!如果还有其他需要添加的内容,请告诉我。您也可以将其更改为类似的内容
var usr = users.AsQueryable().Where(u => u.UserName.Equals(username)).FirstOrDefault();
问题确实存在于方法GetCollection()中。一旦我用以下代码替换它,它就工作得很好:
public MongoCollection<T> GetCollection<T>(string name = null)
{
string collectionName = name;
if (collectionName == null)
collectionName = typeof(T).Name;
if (Database.CollectionExists(collectionName) == false)
Database.CreateCollection(collectionName);
return Database.GetCollection<T>(collectionName);
}
public MongoCollection GetCollection(字符串名称=null)
{
字符串collectionName=name;
if(collectionName==null)
collectionName=typeof(T).Name;
if(Database.CollectionExists(collectionName)==false)
Database.CreateCollection(collectionName);
返回Database.GetCollection(collectionName);
}
“程序刚刚停止”异常?停止意味着什么?它意味着此时,程序不会继续。linq语句不会返回。我希望我知道为什么或者这意味着什么。你在使用MongoDB C#Linq驱动程序吗?请尝试使用探查器检查哪些查询转到DB server()。这两个选项都给了我错误。模型。用户不包含定义……是否缺少程序集引用?我正在使用System.Linq和Newtonsoft.Json.Linq两者都会导致问题?请尝试users.AsQueryable()。其中…我认为问题在于GetCollection方法。毕竟,它似乎并没有得到一个用户集合。这就是linq语句失败的原因,因为没有用户可以查询。我只需要检查数据库是否存在,如果不存在,就创建它