Search code examples
razorasp.net-webpages

How to do MD5 hashing in ASP.NET web pages (razor syntax/cshtml)


I am converting a PHP page into ASP.NET web pages with RAZOR syntax(cshtml). I know how to use MD5 in c# but cannot get any help to do in RAZOR syntax. can someone help me to convert following PHP line into Razor syntax.

if(isset($_POST['login-check']) && $_POST['login-check'] == 'request'){

$CampusName = $_POST['campusid'];
$studentID = $_POST['studentid'];
$pass = $_POST['password'];
$salt = "portal";
$pass = md5($pass.''.$salt);

}

this line of code

$pass = md5($pass.''.$salt);

Regards.


Solution

  • Razor syntax is C#. But you shouldn't be using MD5 to hash passwords. It's a broken algorithm. The Web Pages framework includes a Crypto helper class with a HashPassword method that uses a proper algorithm: https://www.mikesdotnetting.com/article/200/the-simplemembershipprovider-secure-passwords-and-the-crypto-helper. If you don't want to use the SimpleMembershipProvider for managing user accounts, you can use the helper methods on their own:

    if(IsPost()){
        var CampusName = Request["campusid"];
        var studentID = Request["studentid"];
        var pass = Crypto.HashPassword(Request["password"]);
        // no need for messing about with salts, already taken care of
    }
    

    Then you use the VerifyHashedPassword method to compare a submitted password with the hashed value that you stored.