我正在尝试使用 Razor 对名为 users (containinig an id(int) username(nchar(10)
) and的表进行非常简单的用户名和密码验证,password(char(32)
但我遇到了这个错误:
Operator '==' cannot be applied to operands of type 'WebMatrix.Data.DynamicRecord' and 'HasherMD5Main'
HasherMD5Main
我创建了一个.cs类,它执行 md5 哈希(在数据库中。
代码
@{// Initialize page
var enteredusername = "";
var enteredpassword = "";
var ErrorMessage = "";
// If this is a POST request, validate and process data
if( IsPost ) {
enteredusername = Request.Form["enteredusername"];
enteredpassword = Request.Form["enteredpassword"];
var hash = new HasherMD5Main( enteredpassword );
if( enteredusername.IsEmpty() || enteredpassword.IsEmpty() ) {
ErrorMessage = "You must specify a username and password.";
}
else {
var db = Database.Open( "MyConnectionString" );
var passwordquery = db.QuerySingle( "SELECT password FROM users WHERE username = @0", enteredusername );
if( passwordquery == hash ) {
Response.Redirect( "/Success" );
}
else {
Response.Redirect( "/Failure" );
}
}
}
}
@if( ErrorMessage != "" ) {
<p>@ErrorMessage</p>
<p>Please correct the errors and try again.</p>
}
<form method="post" action="">
<fieldset>
<legend>Log In to Your Account</legend>
<ol>
<li>
<label>Username:</label>
<input type="text" id="enteredusername" name="enteredusername" />
</li>
<li>
<label>Password:</label>
<input type="password" id="enteredpassword" name="enteredpassword" />
</li>
<li>
<p>
<input type="submit" value="login" /></p>
</li>
</ol>
</fieldset>
</form>
关于如何简单地验证我的应用程序提供的散列密码是否与存储在数据库中的散列匹配的任何想法?