2016-01-01 48 views
1

我正在做一個簡單的新聞提要,我從一個隱藏在我手動輸入的網址(沒有帳戶功能)的表單中輸入一個新項目。但我想要一個額外的防線,如果找到了表格,所以我添加了一個密碼字段,所以如果密碼匹配預設,我有這種形式將數據保存到一個XML文件。簡單的密碼驗證c#MVC

現在的問題是,這裏最好的做法是做出驗證,我在哪裏放置密碼?

目前我的代碼看起來是這樣的:

[HttpPost] 
[ValidateAntiForgeryToken()] 
public ActionResult AddPost(AddPostModel model) 
{ 
    if (ModelState.IsValid && model.Password == "MyPassword") 
    { 
     AddPostModel.AddPostToXML(model); 
    } 
    return RedirectToAction("Index"); 
} 

的事情是,它的一個小網站,並在最壞的情況,他們補充說,不應該存在的新聞項目。那麼我是否需要採取額外的預防措施,或者是否足夠安全以保護它應該保護的內容?

由於我很新,我沒有很多安全經驗,所以任何指導方針或要記住的內容也將非常感激。

謝謝!

回答

1

一些討論,我在具有在web.config中,我再覈對,看是否密碼是正確的哈希密碼沉降之後。然後在檢查過程中,我只是使用相同的函數對輸入的密碼進行哈希處理,並檢查它是否匹配。

這是我創建的課程,如果其他人正在尋找類似的東西。 =)

public class Security 
{ 
    public static bool ValidatePassword(string password) 
    { 
     string hashValue = HashPassword(password); 

     if (hashValue == ConfigurationManager.AppSettings["password"]) 
     { 
      return true; 
     } 

     return false; 

    } 

    private static string HashPassword(string passwordToHash) 
    { 
     HashAlgorithm hash = new SHA256Managed(); 
     byte[] plainTextBytes = System.Text.Encoding.UTF8.GetBytes(passwordToHash); 
     byte[] hashBytes = hash.ComputeHash(plainTextBytes); 

     //in this string you got the encrypted password 
     return Convert.ToBase64String(hashBytes); 
    } 
} 
0

我發現有用的鏈接,可能會幫助你有一個定製安全級別的想法http://www.c-sharpcorner.com/uploadfile/jitendra1987/password-validator-in-C-Sharp/ 你看過Ajax工具包嗎?他們有很好的機制來設置你的第一道安全防線,即密碼長度,增加複雜性和其他功能。請看看:http://www.ajaxcontroltoolkit.com/PasswordStrength/PasswordStrength.aspx

+0

問題不在於我需要驗證新密碼,而是驗證輸入的密碼是否與預設的密碼相同,當我爲該網站輸入新聞項目時設置了該密碼。 – Threezool