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
Solved

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

Posted on 2011-02-17
4
388 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
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:Mamtha1982
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

792 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