MD5.Create() 抛出 NullRefrence
MD5.Create() throws NullRefrence
以下行抛出 NullRefrenceException
:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void newUser(string name, string password,string email)
{
var db = new ORMDataContext();
var user = new User();
Console.Write(password);
user.email = email;
user.password = MD5.Create(password).ToString();
user.username = name;
db.Users.InsertOnSubmit(user);
db.SubmitChanges();
this.Context.Response.ContentType = "application/json; charset=utf-8";
this.Context.Response.Write(JsonConvert.SerializeObject(user.Id));
}
我检查了一下 password 不为空,不知何故 md5-ing 它 returns null。
那是因为MD5.Create
期望的参数是算法名称。
这样修改使用
using (MD5 md5 = MD5.Create())
{
md5.ComputeHash(Encoding.UTF8.GetBytes(passowrd));// logic
}
这不是您使用 MD5
class 的方式。试试这个:
using(MD5 md5Hash = MD5.Create())
{
user.password =
Convert.ToBase64String(md5Hash.ComputeHash(Encoding.UTF8.GetBytes(password)));
}
以下行抛出 NullRefrenceException
:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void newUser(string name, string password,string email)
{
var db = new ORMDataContext();
var user = new User();
Console.Write(password);
user.email = email;
user.password = MD5.Create(password).ToString();
user.username = name;
db.Users.InsertOnSubmit(user);
db.SubmitChanges();
this.Context.Response.ContentType = "application/json; charset=utf-8";
this.Context.Response.Write(JsonConvert.SerializeObject(user.Id));
}
我检查了一下 password 不为空,不知何故 md5-ing 它 returns null。
那是因为MD5.Create
期望的参数是算法名称。
这样修改使用
using (MD5 md5 = MD5.Create())
{
md5.ComputeHash(Encoding.UTF8.GetBytes(passowrd));// logic
}
这不是您使用 MD5
class 的方式。试试这个:
using(MD5 md5Hash = MD5.Create())
{
user.password =
Convert.ToBase64String(md5Hash.ComputeHash(Encoding.UTF8.GetBytes(password)));
}