2

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.

1
  • Welcome to StackOverflow. This question is way too broad. Please narrow it down. We are not a free coding service. Commented May 30, 2016 at 17:46

6 Answers 6

2
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.

Sign up to request clarification or add additional context in comments.

Comments

1

What exactly do you wanna hear now?

  1. Create the login form
  2. Let the user enter his credentials
  3. Connect to the database
  4. Check whether the entered data is correct
  5. Allow access to the application if 4 is true, otherwise deny access

2 Comments

can u please write the source for all those?
Marius, I think he is offering you a job. Please quote him your hourly rate.
0

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

which part? i mean this is whole project and which part are you interested in?
0

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

0

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

-1

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;               
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.