I have a login page and a welcome page. I have saved user details in my database.
It is working fine but problem is that user can go to the welcome page without login by changing the url of the webpage. How to set that without login user can not go to welcome page.
Here is my login page code-
Login.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
public partial class Login : System.Web.UI.Page
{
string con_string = ConfigurationManager.ConnectionStrings["testAzharConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click1(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(con_string);
string query = ("select count(*) from UserProfile where UserId ='" + txtUserId.Text + "' and Password='" + txtPassword.Text + "'");
SqlCommand cmd = new SqlCommand(query, con);
cmd.Connection = con;
con.Open();
int u = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
Captcha1.ValidateCaptcha(txtCaptcha.Text.Trim());
if (u > 0 && Captcha1.UserValidated)
{
Response.Cookies["txtUserName"].Value = txtUserId.Text;
Response.Redirect("Main.aspx");
}
else if (u == 0)
{
lblCaptcha.Text = "Unauthorized User";
txtCaptcha.Text = "";
txtUserId.Text = "";
txtPassword.Text = "";
}
else
{
lblCaptcha.ForeColor = System.Drawing.Color.Red;
lblCaptcha.Text = "You have Entered InValid Captcha Characters please Enter again";
txtCaptcha.Text = "";
}
}
}
Login.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>
<%@ Register Assembly="MSCaptcha" Namespace="MSCaptcha" TagPrefix="rsv" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Login</title>
<link rel="Stylesheet" href="StyleSheet.css" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<h1>Expense Management</h1>
<h3>Please Login to manage Company Expenses.</h3>
<table align="center" border="2" width="300">
<tr>
<td>User Id:</td>
<td><asp:TextBox ID="txtUserId" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Password</td>
<td><asp:TextBox ID="txtPassword" TextMode="Password" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2">
<rsv:CaptchaControl ID="Captcha1" runat="server" CaptchaLength="5"
CaptchaHeight="60" CaptchaMinTimeout="5" CaptchaMaxTimeout="200"
ForeColor="#00FFCC" BackColor="White" CaptchaChars="ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
FontColor="Red" Width="177px"/>
</td>
</tr>
<tr>
<td>Enter Captcha:</td>
<td><asp:TextBox ID="txtCaptcha" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td><asp:Button ID="Button1" runat="server" Text="Login" onclick="Button1_Click1" /></td>
<td><asp:Label ID="lblCaptcha" runat="Server" ForeColor="Red"></asp:Label></td>
</tr>
<tr>
<td>
<asp:HyperLink ID="linkForgetPassword" runat="server" ForeColor="Red" NavigateUrl="~/ForgetPassword.aspx">Forget Password ?</asp:HyperLink></td>
</tr>
</table>
</form>
</body>
</html>
Please tell me how to set security on my login page.