Solved

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

Posted on 2011-02-17
4
387 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
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.

778 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