C# MVC.Net自定义注册/登录错误“;salt的格式不是{int}.{string}";
我需要你的帮助。我正在尝试在MVC.Net中进行自定义注册/登录,它使用SimpleCripto来加密密码。在我注册了一个用户之后,所有的内容都保存在我的中,似乎没有问题,但是当我尝试登录时,我得到了一个错误——“salt的格式不是预期的{int}.{string}”,它来自我的“IsValid”方法,在语句“if(user.Password==crypto.Compute(user.PasswordSalt,Password))”中。我将发布我的AuthenticationController以及注册和登录方法,如果您能指出问题所在以及如何解决,我将不胜感激。提前谢谢C# MVC.Net自定义注册/登录错误“;salt的格式不是{int}.{string}";,c#,asp.net,asp.net-mvc,entity-framework,C#,Asp.net,Asp.net Mvc,Entity Framework,我需要你的帮助。我正在尝试在MVC.Net中进行自定义注册/登录,它使用SimpleCripto来加密密码。在我注册了一个用户之后,所有的内容都保存在我的中,似乎没有问题,但是当我尝试登录时,我得到了一个错误——“salt的格式不是预期的{int}.{string}”,它来自我的“IsValid”方法,在语句“if(user.Password==crypto.Compute(user.PasswordSalt,Password))”中。我将发布我的AuthenticationController
namespace Final.Controllers
{
public class AuthenticationController : Controller
{
[HttpGet]
public ActionResult LogIn()
{
return View();
}
[HttpPost]
public ActionResult LogIn(Models.User user)
{
if (IsValid(user.Email, user.Password))
{
FormsAuthentication.SetAuthCookie(user.Email, false);
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError("", "Login details are wrong.");
}
return View(user);
}
[HttpGet]
public ActionResult Register()
{
return View();
}
[HttpPost]
public ActionResult Register(Models.User user)
{
try
{
if (ModelState.IsValid)
{
using (AppContext db = new AppContext())
{
var crypto = new SimpleCrypto.PBKDF2();
var encrypPass = crypto.Compute(user.Password);
var newUser = db.Users.Create();
newUser.FirstName = user.FirstName;
newUser.LastName = user.LastName;
newUser.Email = user.Email;
newUser.CompanyName = user.CompanyName;
newUser.Password = encrypPass;
newUser.PasswordSalt = crypto.Salt;
newUser.AdminCode = 0;
user.Password = encrypPass;
user.PasswordSalt = crypto.Salt;
db.Users.Add(newUser);
db.SaveChanges();
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError("", "Data is not correct");
}
}
catch (DbEntityValidationException e)
{
foreach (var validationErrors in e.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
Trace.TraceInformation(
"Class: {0}, Property: {1}, Error: {2}",
validationErrors.Entry.Entity.GetType().FullName,
validationError.PropertyName,
validationError.ErrorMessage);
}
}
}
return View();
}
private bool IsValid(string email, string password)
{
var crypto = new SimpleCrypto.PBKDF2();
bool IsValid = false;
using (AppContext db = new AppContext())
{
var user = db.Users.FirstOrDefault(u => u.Email == email);
if (user != null)
{
if (user.Password == crypto.Compute(user.PasswordSalt, password))
{
IsValid = true;
}
}
}
return IsValid;
}
public ActionResult LogOut()
{
FormsAuthentication.SignOut();
return RedirectToAction("Index", "Home");
}
}
}
请检查
crypto.Compute
函数参数。它需要textToHash(这是您的密码)和salt。您必须交换参数
您需要修改IsValid
函数,如下所示:
private bool IsValid(string email, string password)
{
var crypto = new SimpleCrypto.PBKDF2();
bool IsValid = false;
using (AppContext db = new AppContext())
{
var user = db.Users.FirstOrDefault(u => u.Email == email);
if (user != null)
{
if (user.Password == crypto.Compute(password, user.PasswordSalt))
{
IsValid = true;
}
}
}
return IsValid;
}
谢谢你的快速回复。它做到了,但我没有足够的声誉给你投票!所以问题在于参数的顺序,对吗?再次感谢:)