Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.Data;
  6. using System.Data.SqlClient;
  7. using System.Linq;
  8. using System.Web;
  9.  
  10. namespace WebApplication3
  11. {
  12. public class Sessionmanager
  13. {
  14. public int Id;
  15. public string Name;
  16. public bool Isexterminator;
  17. public string Password;
  18. public Sessionmanager(string name, string password) //doubles as storage for initially submitted user info
  19. {
  20. this.Id = 0;
  21. this.Name = name;
  22. this.Password = password;
  23. this.Isexterminator = false; //todo:make check
  24. }
  25.  
  26. public bool Login()
  27. {
  28. ArrayList items = new ArrayList();
  29.  
  30. using (SqlConnection cn = new SqlConnection
  31. { ConnectionString = ConfigurationManager.ConnectionStrings["CustomerConnectionString"].ToString() })
  32. {
  33. using (SqlCommand cmd = new SqlCommand
  34. {
  35. Connection = cn,
  36. CommandText = "SELECT * FROM customers WHERE [name] = @name AND [password] = @password",
  37. })
  38.  
  39. {
  40. cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = this.Name;
  41. cmd.Parameters.Add("@password", SqlDbType.VarChar).Value = this.Password;
  42. cn.Open();
  43.  
  44. var Reader = cmd.ExecuteReader();
  45. if (Reader.HasRows)
  46. {
  47. while (Reader.Read())
  48. {
  49. string zip = Reader.GetString(Reader.GetOrdinal("zip"));
  50. string name = Reader.GetString(Reader.GetOrdinal("name"));
  51. string pass = Reader.GetString(Reader.GetOrdinal("password"));
  52. string phone = Reader.GetString(Reader.GetOrdinal("phone")); //needs to be null checked - can be null
  53. string email = Reader.GetString(Reader.GetOrdinal("email"));
  54. int isexterminator = Reader.GetInt32(Reader.GetOrdinal("isexterminator"));
  55. int id = Reader.GetInt32(Reader.GetOrdinal("id"));
  56. //int id = Reader.GetInt32(Reader.GetOrdinal("id"));
  57. // bool isexterminator name = Reader.GetBoolean(Reader.GetOrdinal("isexterminator")); // changed to int 0
  58.  
  59. Customer c = new Customer(zip.ToString(), name, pass, "fakephone", email);
  60. items.Add(c);
  61.  
  62. HttpContext context = HttpContext.Current;
  63. context.Session["userId"] = id;
  64. context.Session["userIsExterminator"] = isexterminator;
  65. }
  66. }
  67. Reader.Close();
  68. }
  69. }
  70.  
  71. if (items.Count == 1)
  72. {
  73. HttpContext context = HttpContext.Current;
  74. context.Session["userName"] = this.Name;
  75.  
  76.  
  77.  
  78. return true;
  79. }
  80. else
  81. {
  82. return false;
  83. }
  84. }
  85.  
  86. public static bool CurrentSessionIsExterminator()
  87. {
  88. string loggedinusername;
  89. HttpContext context = HttpContext.Current;
  90. loggedinusername = (string)(context.Session["userName"]);
  91.  
  92. if (loggedinusername != null && loggedinusername.Length > 0)
  93. {
  94. /* sessionvalue tests - working
  95. Response.Write((int)(context.Session["userId"]));
  96. Response.Write((int)(context.Session["userIsExterminator"]));
  97. */
  98. int uid = (int)(context.Session["userId"]);
  99. int uiex = (int)(context.Session["userIsExterminator"]);
  100. if (uiex == 1)
  101. {
  102. return true;
  103. }
  104. else
  105. {
  106. return false;
  107. }
  108.  
  109. }
  110. else { return false; }
  111. }
  112.  
  113. public static void LogOut()
  114. {
  115. HttpContext context = HttpContext.Current;
  116. context.Session.Abandon(); //clear session
  117. System.Web.Security.FormsAuthentication.SignOut(); //clear session cookie
  118. }
  119.  
  120. }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement