0

I am writing an unencryption program and the business requirement is that two users must login in order for it to work. How can I write program that in C# that attempts to log both users into SQL? The users are SQL Users and I can't figure out how to authenticate SQL Users in C#.

I imagined a login screen with two user name slots and two password slots and then a shared login button. On click, user authentication happens for each user. If both authentications are successful, the file is decrypted.

Any help, advice or direction would be greatly appreciated.

Missy
  • 1,286
  • 23
  • 52

2 Answers2

3

Something Like this can do the trick:

private async Task TestUserAuthAsync(){

    var task1 = Task.Run(AuthenticateUser("username1", "password1"));
    var task2 = Task.Run(AuthenticateUser("username2", "password2"));

    await Task.WhenAll(task1, task2);
}

You can do more, like return results log...

Something like Parallel.ForEach can also work.

rmjoia
  • 962
  • 12
  • 21
  • Is AuthenticateUser a built in SQL function? – Missy Sep 26 '17 at 11:58
  • No, I was making up some method. you'll need to write code for that.. maybe try using linq to sql for instance.. I thought you already had the code to authenticate one user – rmjoia Sep 26 '17 at 12:00
  • They want to use the users that you set up in SQL. Still upticked you for helping me :) – Missy Sep 26 '17 at 12:01
  • 1
    Sure, you can fetch all (filtered) users from SQL and then run the method. In that case I would get the users to a list, and then run a foreach. Check [Introduction to Entity Framework](https://msdn.microsoft.com/en-us/library/aa937723(v=vs.113).aspx) – rmjoia Sep 26 '17 at 12:03
  • Okay -- I'll check it out and see if it gets me enough information to authenticate them. – Missy Sep 26 '17 at 12:10
  • Awesome. Thank you. I will work this on my own as much as possible first. – Missy Sep 26 '17 at 12:30
1

The Authenticate User Method would look something like this - note that the connection strings need to be modified for the specific app and the MessageBox in the Catch statement is probably too much for the average user :)

 private bool AuthenticateUser() 
     {
        // rather than picking up variable names, I am picking up the information from the text boxes on the screen.
        bool retVal = false;
        string cs1 = "Data Source=yourdatasource;Initial Catalog=yourcat;Persist Security Info=True;User ID=" + txtLogin1.Text.Trim() + ";Password=" + txtPW1.Text.Trim() + ";";
        string cs2 = "Data Source=yourdatasource;Initial Catalog=yourcat;Persist Security Info=True;User ID=" + txtLogin2.Text.Trim() + ";Password=" + txtPW2.Text.Trim() + ";";
        try
        {
            using (var connection = new SqlConnection(cs1))
            {
                connection.Open();
                retVal = true;
            }
        }
        catch (SqlException e2)
        {
            MessageBox.Show("User 1 Failed " + e2.ToString());
        }

        try
        {
            using (var connection = new SqlConnection(cs2))
            {
                connection.Open();
                retVal = true;
            }
        }
        catch (SqlException ex)
        {
            MessageBox.Show("User 2 Failed "+ex.ToString());

        } 
        if (retVal)
            { MessageBox.Show("Passed"); }
        else
            { MessageBox.Show("Please Try"); }

        return retVal;
    }
}
Missy
  • 1,286
  • 23
  • 52