-
Notifications
You must be signed in to change notification settings - Fork 1
/
login.aspx.cs
90 lines (87 loc) · 3.34 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
namespace istracker_asp.net
{
public partial class login : System.Web.UI.Page
{
static string DatabaseConnectionString = ConfigurationManager.ConnectionStrings["dbConStr"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
Session.RemoveAll();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
try {
using (SqlConnection myConnection = new SqlConnection(DatabaseConnectionString))
{
myConnection.Open();
string stmt = "SELECT password FROM users WHERE username=@username;";
using (SqlCommand cmdCount = new SqlCommand(stmt, myConnection))
{
cmdCount.Parameters.AddWithValue("username", userName.Text.ToLower());
using (SqlDataReader reader = cmdCount.ExecuteReader())
{
if (reader.Read())
{
if (reader.GetString(0) == password.Text)
{
Session["login"] = true;
Session["username"] = userName.Text.ToLower();
Response.Redirect("torrent.aspx");
return;
}
}
}
}
}
} catch
{
Error.Text = "Unable to connecto db";
}
Error.Text = "Wrong username or password";
}
protected void Button2_Click(object sender, EventArgs e)
{
if (userName.Text.Length == 0 || password.Text.Length == 0)
{
Error.Text = "Username too short";
}
try {
using (SqlConnection myConnection = new SqlConnection(DatabaseConnectionString))
{
myConnection.Open();
string stmt = "INSERT INTO users (username, password) values(@username, @password);";
try
{
using (SqlCommand myCommand = new SqlCommand(stmt, myConnection))
{
myCommand.Parameters.AddWithValue("username", userName.Text.ToLower());
myCommand.Parameters.AddWithValue("password", password.Text);
myCommand.ExecuteNonQuery();
Session["login"] = true;
Session["username"] = userName.Text.ToLower();
Response.Redirect("torrent.aspx");
}
}
catch
{
Error.Text = "Unable to add user";
}
}
} catch
{
Error.Text = "Unable to connecto db";
}
}
}
}