2016-12-13 5 views
1

私はあなたの助けが必要です。私はパスワードを暗号化するためにSimpleCriptoを使用するMVC.Netでカスタム登録/ログインをしようとしています。私はユーザーを登録した後、すべてが私のtableに保存されているようですが、これは間違いないようですが、LogInにしようとすると「塩が{int}。{string}の予想されたフォーマットではありませんでした私の "IsValid"メソッドは、 "if(user.Password == crypto.Compute(user.PasswordSalt、password))"という文の中にあります。 AuthenticantionControllerをRegisterメソッドとLogInメソッドで公開します。問題がどこにあるのか、それを解決する方法を指摘できる場合は、感謝します。前もって感謝します !MVC.Netカスタム登録/ログインエラー "塩が{int} {string}の予期された形式ではありませんでした"

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"); 
     } 
    } 
} 

答えて

1

crypto.Compute機能パラメータを確認してください。それはあなたのパスワードであるtextToHashと塩が必要です。パラメータを交換する必要があります。

あなたはこのようなあなたの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; 
} 
+1

感謝を。それは仕事でしたが、私はあなたに投票を与えるために十分な評判を持っていません!だから、問題はパラメータの順になっていたのですよね?もう一度ありがとう :) –

関連する問題