Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Data;
- using System.Data.SqlClient;
- using System.Linq;
- using System.Web;
- namespace WebApplication3
- {
- public class Sessionmanager
- {
- public int Id;
- public string Name;
- public bool Isexterminator;
- public string Password;
- public Sessionmanager(string name, string password) //doubles as storage for initially submitted user info
- {
- this.Id = 0;
- this.Name = name;
- this.Password = password;
- this.Isexterminator = false; //todo:make check
- }
- public bool Login()
- {
- ArrayList items = new ArrayList();
- using (SqlConnection cn = new SqlConnection
- { ConnectionString = ConfigurationManager.ConnectionStrings["CustomerConnectionString"].ToString() })
- {
- using (SqlCommand cmd = new SqlCommand
- {
- Connection = cn,
- CommandText = "SELECT * FROM customers WHERE [name] = @name AND [password] = @password",
- })
- {
- cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = this.Name;
- cmd.Parameters.Add("@password", SqlDbType.VarChar).Value = this.Password;
- cn.Open();
- var Reader = cmd.ExecuteReader();
- if (Reader.HasRows)
- {
- while (Reader.Read())
- {
- string zip = Reader.GetString(Reader.GetOrdinal("zip"));
- string name = Reader.GetString(Reader.GetOrdinal("name"));
- string pass = Reader.GetString(Reader.GetOrdinal("password"));
- string phone = Reader.GetString(Reader.GetOrdinal("phone")); //needs to be null checked - can be null
- string email = Reader.GetString(Reader.GetOrdinal("email"));
- int isexterminator = Reader.GetInt32(Reader.GetOrdinal("isexterminator"));
- int id = Reader.GetInt32(Reader.GetOrdinal("id"));
- //int id = Reader.GetInt32(Reader.GetOrdinal("id"));
- // bool isexterminator name = Reader.GetBoolean(Reader.GetOrdinal("isexterminator")); // changed to int 0
- Customer c = new Customer(zip.ToString(), name, pass, "fakephone", email);
- items.Add(c);
- HttpContext context = HttpContext.Current;
- context.Session["userId"] = id;
- context.Session["userIsExterminator"] = isexterminator;
- }
- }
- Reader.Close();
- }
- }
- if (items.Count == 1)
- {
- HttpContext context = HttpContext.Current;
- context.Session["userName"] = this.Name;
- return true;
- }
- else
- {
- return false;
- }
- }
- public static bool CurrentSessionIsExterminator()
- {
- string loggedinusername;
- HttpContext context = HttpContext.Current;
- loggedinusername = (string)(context.Session["userName"]);
- if (loggedinusername != null && loggedinusername.Length > 0)
- {
- /* sessionvalue tests - working
- Response.Write((int)(context.Session["userId"]));
- Response.Write((int)(context.Session["userIsExterminator"]));
- */
- int uid = (int)(context.Session["userId"]);
- int uiex = (int)(context.Session["userIsExterminator"]);
- if (uiex == 1)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- else { return false; }
- }
- public static void LogOut()
- {
- HttpContext context = HttpContext.Current;
- context.Session.Abandon(); //clear session
- System.Web.Security.FormsAuthentication.SignOut(); //clear session cookie
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement