Advertisement
Guest User

dekripcija

a guest
Nov 14th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.02 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data.SqlClient;
  6. using System.Data;
  7.  
  8. namespace Common
  9. {
  10. public class User
  11. {
  12. public static string LoggedInUser;
  13. private long _id;
  14.  
  15. public long Id
  16. {
  17. get { return _id; }
  18. set { _id = value; }
  19. }
  20. private string _name;
  21.  
  22. public string Name
  23. {
  24. get { return _name; }
  25. set { _name = value; }
  26. }
  27. private string _username;
  28.  
  29.  
  30.  
  31. public string Username
  32. {
  33. get { return _username; }
  34. set { _username = value; }
  35. }
  36.  
  37.  
  38. private string _password;
  39.  
  40. public string Password
  41. {
  42. get { return _password; }
  43. set { _password = value; }
  44. }
  45. //private long? _cashAccountId;
  46.  
  47. //public long? CashAccountId
  48. //{
  49. // get { return _cashAccountId; }
  50. // set
  51. // {
  52. // _cashAccountId = value;
  53. // CashAccount = new Account();
  54. // }
  55. //}
  56.  
  57. //public Account CashAccount;
  58.  
  59. private long? _clientId;
  60.  
  61. public long? ClientId
  62. {
  63. get { return _clientId; }
  64. set
  65. {
  66. _clientId = value;
  67. Client = new Client();
  68. }
  69. }
  70.  
  71. public Client Client { get; set; }
  72.  
  73. public User(string name, string username, string password, long? cashAccountId, long? clientId)
  74. {
  75. Name = name;
  76. Username = username;
  77. Password = password;
  78. //CashAccountId = cashAccountId;
  79. ClientId = clientId;
  80. }
  81.  
  82.  
  83. public User()
  84. { }
  85.  
  86.  
  87. public static bool CheckLogin(string username, string password, out string msg)
  88. {
  89. if (String.IsNullOrEmpty(username) || String.IsNullOrEmpty(password))
  90. {
  91. msg = "Invalid username or password!";
  92. return false;
  93. }
  94.  
  95. try
  96. {
  97. //Create SqlConnection
  98. using (SqlConnection con = new SqlConnection(Common.connectionString))
  99. {
  100. using (SqlCommand cmd = new SqlCommand("dbo.CheckLogin", con))
  101. {
  102. cmd.CommandType = CommandType.StoredProcedure;
  103. cmd.Parameters.AddWithValue("@username", username);
  104. cmd.Parameters.AddWithValue("@password", password);
  105.  
  106. SqlParameter param = new SqlParameter("@res", SqlDbType.Bit);
  107. //cmd.Parameters.Add("@res", SqlDbType.Bit);
  108. param.Direction = ParameterDirection.ReturnValue;
  109. cmd.Parameters.Add(param);
  110.  
  111. cmd.Parameters.Add("@userId",SqlDbType.BigInt).Direction = ParameterDirection.Output;
  112.  
  113. con.Open();
  114. cmd.ExecuteNonQuery();
  115.  
  116. object res = param.Value;
  117.  
  118. if ((int)res == 1)
  119. {
  120. long userId = Convert.ToInt64(cmd.Parameters["@userId"].Value);
  121. Common.currentUserId = userId;
  122. Common.changeLoggedInUser(username);
  123. msg = "Login Successful!";
  124. return true;
  125. }
  126. else
  127. {
  128. msg = "Login Failed!";
  129. return false;
  130. }
  131. }
  132. }
  133. }
  134. catch (Exception ex)
  135. {
  136. msg = ex.Message;
  137. return false;
  138. }
  139. }
  140. public static bool RegisterUser(string username, string password, string confirm, out string msg)
  141. {
  142. SqlConnection con = new SqlConnection(Common.connectionString);
  143. if (username != "" && password != "" && confirm != "") //validating the fields whether the fields or empty or not
  144. {
  145. if (password.Trim().ToLower() == confirm.Trim().ToLower()) //validating Password textbox and confirm password textbox is match or unmatch
  146. {
  147. string UserName = username;
  148. string Password = Cryptography.Encrypt(password); // Passing the Password to Encrypt method and the method will return encrypted string and stored in Password variable.
  149. // con.Open();
  150. SqlCommand insert = new SqlCommand("insert into Users(UserName,[Password]) values('" + UserName + "','" + Password + "')", con);
  151. con.Open();
  152. insert.ExecuteNonQuery();
  153. con.Close();
  154. msg = "Record for "+UserName+" inserted successfully";
  155. LoggedInUser = UserName;
  156. Common.changeLoggedInUser(UserName);
  157. return true;
  158. // MessageBox.Show("Record inserted successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
  159. }
  160. else
  161. {
  162. msg = "Password and Confirm Password doesn't match!.. Please Check..";
  163. return false;
  164. //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
  165. }
  166. }
  167. else
  168. {
  169.  
  170. msg = "Please fill all the fields!..";
  171. return false;
  172. // MessageBox.Show("Please fill all the fields!..", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); //showing the error message if any fields is empty
  173. }
  174. }
  175. public static bool LoginWithDecrypt(string username, string password, out string msg)
  176. {
  177. SqlConnection con = new SqlConnection(Common.connectionString);
  178. string Password = "";
  179. bool IsExist = false;
  180. con.Open();
  181. SqlCommand cmd = new SqlCommand("select * from Users where UserName='" + username + "'", con);
  182. SqlDataReader sdr = cmd.ExecuteReader();
  183. if (sdr.Read())
  184. {
  185. Password = sdr.GetString(2); //get the user password from db if the user name is exist in that.
  186. IsExist = true;
  187. }
  188. con.Close();
  189. if (IsExist) //if record exis in db , it will return true, otherwise it will return false
  190. {
  191. if (Cryptography.Decrypt(Password).Equals(password))
  192. {
  193. msg = "Successful login!";
  194. LoggedInUser = username;
  195. Common.changeLoggedInUser(username);
  196. return true;
  197. // MessageBox.Show("Login Success", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
  198.  
  199. }
  200. else
  201. {
  202. msg = "Incorrect password!";
  203. return false;
  204. // MessageBox.Show("Password is wrong!...", "error", MessageBoxButtons.OK, MessageBoxIcon.Information);
  205. }
  206.  
  207. }
  208. else //showing the error message if user credential is wrong
  209. {
  210.  
  211. msg = "Invalid credentials";
  212. return false;
  213. //MessageBox.Show("Please enter the valid credentials", "error", MessageBoxButtons.OK, MessageBoxIcon.Information);
  214. }
  215. }
  216.  
  217. }
  218.  
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement