Solved

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

Posted on 2011-02-17
4
385 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
4 Comments
 
LVL 53

Expert Comment

by:Dhaest
Comment Utility
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:Mamtha1982
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now