patterncsharpMinor
Check assistant's password
Viewed 0 times
assistantcheckpassword
Problem
I have two more methods the same as this except that they query a different table. Is there another way to know if the result of the query is
If it's not
Is there another way to shorten this code? Keep in mind that I should not have a
null? The hasher.CompareStringToHash() method is not allowed to have a null value.If it's not
null, it proceeds to an else condition, and inside that it checks if the encrypted password and password in the textfield are the same. If it's the same it will go to another else statement.Is there another way to shorten this code? Keep in mind that I should not have a
null value in the hasher.CompareStringToHash(). Or, if possible, restrict calling that method if the result is null and lessen the if statements. How can I make this code cleaner and more efficient?public void CheckAssistantsPassword()
{
DbClassesDataContext myDb = new DbClassesDataContext(dbPath);
var password = (from userAccounts in myDb.Assistants
where userAccounts.Ass_UserName== txtUserName.Text
select userAccounts.Ass_Password).FirstOrDefault();
var hasher = new Hasher() { SaltSize = 16 };
if (password == null)
{
MessageBox.Show("Invalid Account");
}
else
{
bool isOkay = hasher.CompareStringToHash(txtPassword.Text,password);
if (isOkay)
{
MessageBox.Show("You May Now Login");
}
else
{
MessageBox.Show("INVALID PASSWORD");
}
}
}Solution
I believe you're looking for
I would also probably refactor this a bit:
Optionally you can place error checking on your context making sure you can establish a connection. Also, I recommend placing a
String.IsNullOrEmpty.// test for null or empty string
if (!String.IsNullOrEmpty(password) {
}
// -- OR --
// If the hasher call just can't accept null values, you can specify
// an empty string to fall back on like so:
(...LINQ...).FirstOrDefault(String.Empty);I would also probably refactor this a bit:
public void CheckAssistantsPassword()
{
using (DbClassesDataContext myDb = new DbClassesDataContext(dbPath))
{
var password = (from userAccounts in myDb.Assistants
where userAccounts.Ass_UserName == txtUserName.Text
select userAccounts.Ass_Password).FirstOrDefault(String.Empty);
var hasher = new Hasher { SaltSize = 16 };
if (hasher.CompareStringToHash(txtPassword.Text, password))
{
// Success
}
else
{
// Invalid password
}
}
}Optionally you can place error checking on your context making sure you can establish a connection. Also, I recommend placing a
using() block around your context to free it up after you're done with it.Code Snippets
// test for null or empty string
if (!String.IsNullOrEmpty(password) {
}
// -- OR --
// If the hasher call just can't accept null values, you can specify
// an empty string to fall back on like so:
(...LINQ...).FirstOrDefault(String.Empty);public void CheckAssistantsPassword()
{
using (DbClassesDataContext myDb = new DbClassesDataContext(dbPath))
{
var password = (from userAccounts in myDb.Assistants
where userAccounts.Ass_UserName == txtUserName.Text
select userAccounts.Ass_Password).FirstOrDefault(String.Empty);
var hasher = new Hasher { SaltSize = 16 };
if (hasher.CompareStringToHash(txtPassword.Text, password))
{
// Success
}
else
{
// Invalid password
}
}
}Context
StackExchange Code Review Q#9597, answer score: 4
Revisions (0)
No revisions yet.