Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Data.SqlClient;
- using System.Data;
- namespace Common
- {
- public class User
- {
- public static string LoggedInUser;
- private long _id;
- public long Id
- {
- get { return _id; }
- set { _id = value; }
- }
- private string _name;
- public string Name
- {
- get { return _name; }
- set { _name = value; }
- }
- private string _username;
- public string Username
- {
- get { return _username; }
- set { _username = value; }
- }
- private string _password;
- public string Password
- {
- get { return _password; }
- set { _password = value; }
- }
- //private long? _cashAccountId;
- //public long? CashAccountId
- //{
- // get { return _cashAccountId; }
- // set
- // {
- // _cashAccountId = value;
- // CashAccount = new Account();
- // }
- //}
- //public Account CashAccount;
- private long? _clientId;
- public long? ClientId
- {
- get { return _clientId; }
- set
- {
- _clientId = value;
- Client = new Client();
- }
- }
- public Client Client { get; set; }
- public User(string name, string username, string password, long? cashAccountId, long? clientId)
- {
- Name = name;
- Username = username;
- Password = password;
- //CashAccountId = cashAccountId;
- ClientId = clientId;
- }
- public User()
- { }
- public static bool CheckLogin(string username, string password, out string msg)
- {
- if (String.IsNullOrEmpty(username) || String.IsNullOrEmpty(password))
- {
- msg = "Invalid username or password!";
- return false;
- }
- try
- {
- //Create SqlConnection
- using (SqlConnection con = new SqlConnection(Common.connectionString))
- {
- using (SqlCommand cmd = new SqlCommand("dbo.CheckLogin", con))
- {
- cmd.CommandType = CommandType.StoredProcedure;
- cmd.Parameters.AddWithValue("@username", username);
- cmd.Parameters.AddWithValue("@password", password);
- SqlParameter param = new SqlParameter("@res", SqlDbType.Bit);
- //cmd.Parameters.Add("@res", SqlDbType.Bit);
- param.Direction = ParameterDirection.ReturnValue;
- cmd.Parameters.Add(param);
- cmd.Parameters.Add("@userId",SqlDbType.BigInt).Direction = ParameterDirection.Output;
- con.Open();
- cmd.ExecuteNonQuery();
- object res = param.Value;
- if ((int)res == 1)
- {
- long userId = Convert.ToInt64(cmd.Parameters["@userId"].Value);
- Common.currentUserId = userId;
- Common.changeLoggedInUser(username);
- msg = "Login Successful!";
- return true;
- }
- else
- {
- msg = "Login Failed!";
- return false;
- }
- }
- }
- }
- catch (Exception ex)
- {
- msg = ex.Message;
- return false;
- }
- }
- public static bool RegisterUser(string username, string password, string confirm, out string msg)
- {
- SqlConnection con = new SqlConnection(Common.connectionString);
- if (username != "" && password != "" && confirm != "") //validating the fields whether the fields or empty or not
- {
- if (password.Trim().ToLower() == confirm.Trim().ToLower()) //validating Password textbox and confirm password textbox is match or unmatch
- {
- string UserName = username;
- string Password = Cryptography.Encrypt(password); // Passing the Password to Encrypt method and the method will return encrypted string and stored in Password variable.
- // con.Open();
- SqlCommand insert = new SqlCommand("insert into Users(UserName,[Password]) values('" + UserName + "','" + Password + "')", con);
- con.Open();
- insert.ExecuteNonQuery();
- con.Close();
- msg = "Record for "+UserName+" inserted successfully";
- LoggedInUser = UserName;
- Common.changeLoggedInUser(UserName);
- return true;
- // MessageBox.Show("Record inserted successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- else
- {
- msg = "Password and Confirm Password doesn't match!.. Please Check..";
- return false;
- //MessageBox.Show("Password and Confirm Password doesn't match!.. Please Check..", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); //showing the error message if password and confirm password doesn't match
- }
- }
- else
- {
- msg = "Please fill all the fields!..";
- return false;
- // MessageBox.Show("Please fill all the fields!..", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); //showing the error message if any fields is empty
- }
- }
- public static bool LoginWithDecrypt(string username, string password, out string msg)
- {
- SqlConnection con = new SqlConnection(Common.connectionString);
- string Password = "";
- bool IsExist = false;
- con.Open();
- SqlCommand cmd = new SqlCommand("select * from Users where UserName='" + username + "'", con);
- SqlDataReader sdr = cmd.ExecuteReader();
- if (sdr.Read())
- {
- Password = sdr.GetString(2); //get the user password from db if the user name is exist in that.
- IsExist = true;
- }
- con.Close();
- if (IsExist) //if record exis in db , it will return true, otherwise it will return false
- {
- if (Cryptography.Decrypt(Password).Equals(password))
- {
- msg = "Successful login!";
- LoggedInUser = username;
- Common.changeLoggedInUser(username);
- return true;
- // MessageBox.Show("Login Success", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- else
- {
- msg = "Incorrect password!";
- return false;
- // MessageBox.Show("Password is wrong!...", "error", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- }
- else //showing the error message if user credential is wrong
- {
- msg = "Invalid credentials";
- return false;
- //MessageBox.Show("Please enter the valid credentials", "error", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement