I am quite new to C# and .NET programming. I am working on an application and want to create a login form for which if the User enters their credentials it checks to see if user details exist in Database and then allows the user access to the application.
6 Answers
private bool IsvalidUser(string userName, string password)
{
DataClasses1DataContext context = new DataClasses1DataContext();
var query = from p in context.EMP
where p.EUSERNAME == userName
&& p.EPassword == password
select p;
if (query.Any())
{
return true;
}
else
{
return false;
}
}
in login button click use this:
if(IsvalidUser(txtUserName.Text,txtPassword.Text)
{
//User is valid
}
This will do the validations for you.
Comments
What exactly do you wanna hear now?
- Create the login form
- Let the user enter his credentials
- Connect to the database
- Check whether the entered data is correct
- Allow access to the application if 4 is true, otherwise deny access
2 Comments
Well, first of all you have to create project with login form, where you have to connect to database, after that, I would make one procedure in my database which would take parameters (user credentials) and it would return Whether the user is. in my login form, i would make one method Login, which would execute that procedure. This method would be used on LoginClick.
1 Comment
I would start looking into a standard authentication and authorisation approach. Have a look at using somthing like AzMan to store users, passwords and their roles etc. this can be done ins an xml file initially and you will be able to encrypt etc. Storing user names and passwords in dbs is questionable.
I appreciate you are new to dot net (and programming in general?) But you might also want to look at splitting your app out a little bit. Ideally your form would do no more than display controls and call other components to implment logic and securty etc. Have a look at UI moels such as MVP, MVC etc. At the very least , wrap your security logic into it's own class:
public SecurityManager
{
public static bool UserIsValid(UserDetails user)
{
//Check here in AzMan or your db
}
public static bool UserIsInRole(string role, UserDetail user)
{
//Check if user is in role (again, in your store or db)
}
}
Your form would simply call the security manager
private void Login_OnClick(object sender, EventArgs e)
{
UserDetails user = new UserDetails(txtusername.Text,txtPassword.Text);
if(SecurityManager.IsValiduser(user))
{
///Ok let them in;;;
}
}
In an ideal world you would introduce a presneter that does the security checking...
I hope this helps.
Comments
Secure applications do not actually store the password. Use code like this to hash the password so that even those who have access to the database cannot determine the original password.
bool ValidateLogin(DataClasses1DataContext context, string user, string password)
{
byte[] providedPasswordHash = hashPassword(password);
byte[] expectedPasswordHash = context.Users.Where(u => u.Name == user).Single().PasswordHash;
if (providedPasswordHash.Length != expectedPasswordHash.Length)
return false;
for(int i = 0; i < providedPasswordHash.Length; i++)
if (providedPasswordHash[i] != expectedPasswordHash[i])
return false;
return true;
}
byte[] hashPassword(string password)
{
System.Security.Cryptography.SHA1CryptoServiceProvider hasher =
new System.Security.Cryptography.SHA1CryptoServiceProvider();
return hasher.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password));
}
Comments
You can try my code
private void login()
{
if (IsvalidUser(txtUsuario.Text, txtPassword.Text))
{
//MessageBox.Show("listo");
Menu ir = new Menu();
ir.lblUsuario.Text = txtUsuario.Text;
this.Hide();
ir.ShowDialog();
}
else
{
MessageBox.Show("Incorrecto, verifique sus datos", "Cecom",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
private bool IsvalidUser(string userName, string password)
{
DatosDataContext context = new DatosDataContext();
var q = from p in context.Usuarios
where p.Usuarios1 == txtUsuario.Text
&& p.Password == txtPassword.Text
select p;
if (q.Any())
{
return true;
}
else
{
return false;
}
}