Solved

how can i create security class for my C#.net window application ?

Posted on 2011-02-17
4
391 Views
Last Modified: 2012-05-11
i want to create security class for my window application , when i start application it will prompt for the password and if user give correct password then he/she login in to application,
otherwise can't login and set other parameter also ,
but how can i do this ?
0
Comment
Question by:Parth48
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 34914530
It's all depending of your requirement. Take a look at the following:

http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_22549287.html
0
 
LVL 12

Expert Comment

by:Praveen Kumar
ID: 34914677
Basically you need to implement a Login form.



string username = txtUserName.Text;
string password = txtPassword.Text;

if (ValidateUserNamePassword(username, password))
{
    // move to next form or do whatever you need to do after a successfull login
}
else
{
    MessageBox.Show("Invalid user name or password", "Invalid Login");
    return;
}


public bool ValidateUserNamePassword(string _username, string _password)
{
       string connectionString = "Data Source=[servername];Initial Catalog=[databaseName];User ID=[Admin Login];Password=[Admin Password];";

       using (SqlConnection cn = new SqlConnection(connectionString);
       {
          SqlCommand cmd = new SqlCommand();
          cmd.Connection = cn;
          cmd.CommandType = CommandType.StoredProcedure;
          cmd.CommandText = "tsp_GetUserNameAndPassword";

          SqlParameterCollection sqlParams = cmd.Parameters;
          sqlParams.AddWithValue("@UserName", _username);
          sqlParams.AddWithValue("@Password", _password);

          cn.Open();
          SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.SingleRow);
          if (dr.Read())
          {
                // this will return true if a row matching the username and password is found.
                // this means that the user's input is valid
                return true;
          }
          else
          {
                return false;
           }

          dr.close();
          cn.close();
       }
}
0
 
LVL 8

Expert Comment

by:cubaman_24
ID: 34924886
Hello:
Don't re-invent the wheel. Use Integrated security or Aspnet memebership. It'll save you a lot of time and troubles. Notice that you can use Aspnet membership system in windows applications too.

http://msdn.microsoft.com/en-us/library/yh26yfzy.aspx

Best regards
0
 
LVL 1

Accepted Solution

by:
diwakarverma earned 500 total points
ID: 34926266
Why you need separate class for logon if you just need to validate user. You can use previous post to just validate user name and password on okay button click of logon dialog.

If you looking for similar feature about how window based authentication woks on ASP.net then you need to implement client application services (please refer http://msdn.microsoft.com/en-us/library/bb384297.aspx for more information).

If you just looking for business class that does authentication then you may go for singleton class like:

Class Logon
{
      Private Logon _logonUser = null;
      Private Logon() {}
      Public GetLoggedOnUser()
      {
            Lock {
                  If (_logonUser == null)      _logonUser  = new Logon();
                  }
            Return _logonUser;
      }
      Public bool LogonUser(string username, string password)
      { // Authenticate User here }
      Public bool LogoutUser()
      {
            // Do some more log off operations
_logonUser = null;
}

The other benefit of having such class that you can have logger (trace info) specified in this class and many others
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question