/
Login.aspx.cs
98 lines (86 loc) · 2.82 KB
/
Login.aspx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
Request.Cookies.Remove("user");
Session.RemoveAll();
}
catch (Exception)
{
}
}
protected void Logbutton_Click(object sender, EventArgs e)
{
if (uname.Value.ToUpper() == "ADMIN" && psw.Value.ToUpper() == "ADMIN@123")
{
Session["id"] = "1";
Session["Type"] = "Admin";
Session["Name"] = "Admin";
Session["IsAuth"] = "true";
Response.Redirect("Home.aspx");
}
else
{
LoginDetails log = ValidateUser(uname.Value, psw.Value);
if (log.IsAuthUser)
{
Session["id"] = log.UserId;
Session["Name"] = log.UserName;
Session["IsAuth"] = log.IsAuthUser;
Session["Type"] = log.Role;
Response.Redirect("Home.aspx");
}
else
{
Response.Redirect("Login.aspx");
}
}
}
private LoginDetails ValidateUser(string username, string password)
{
LoginDetails obj = new LoginDetails();
obj.IsAuthUser = false;
try
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["App"].ConnectionString);
SqlDataAdapter da;
DataSet ds = new DataSet();
string query = "select * from Account where FirstName='" + username.Trim() + "' and Empid='" + password.Trim() + "'";
da = new SqlDataAdapter(query, con);
con.Open();
da.Fill(ds);
// Console.WriteLine(ds.Tables[0].Rows.Count);
if (ds.Tables[0].Rows.Count > 0)
{
obj.IsAuthUser = true;
obj.UserName = ds.Tables[0].Rows[0]["FirstName"].ToString();
obj.UserId = ds.Tables[0].Rows[0]["Empid"].ToString();
obj.Role = ds.Tables[0].Rows[0]["Type"].ToString();
}
}
catch (Exception ex)
{
obj.IsAuthUser = false;
Response.Write("<script>alert('" + ex.Message.Replace("\'", " ") + "')</script>");
}
return obj;
}
private struct LoginDetails
{
public string UserId { get; set; }
public string UserName { get; set; }
public string Role { get; set; }
public bool IsAuthUser { get; set; }
}
}