ASP.Net Login Control

Hello and welcome, here I am writing some interesting point regarding Login Control using ASP.net Login control. It is very much easier to use and convenient for the developer without writing so much code.
Here how it is..

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>

<!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 runat="server">
    <title>Login Panel</title>
    <style type="text/css">
.myLoginPanel
{
background-color:gray;
border-color:blue;
border-style:dotted;
border-width:1px;
font-family:Arial, Verdana;
font-size:10px;   
}
.myLoginButton
{
border-color:blue;
border-style:solid;
border-width:1px;
font-family:Arial, Verdana;
font-size:10px;   
}
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Login ID="myLogin" runat="server" CssClass="myLoginPanel" DisplayRememberMe="false"
    InstructionText="Enter your user name and password to logged in.."
     TitleText="Login Details" UserNameRequiredErrorMessage="Please enter user name" PasswordRequiredErrorMessage="Please enter password"
     onauthenticate="myLogin_Authenticate">
   
    <LoginButtonStyle CssClass="myLoginButton" />
    </asp:Login>
   
    </div>
    </form>
</body>
</html>

.aspx.cs code:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        { }
    }
    protected void myLogin_Authenticate(object sender, AuthenticateEventArgs e)
    {
        string userName = myLogin.UserName;
        string password = myLogin.Password;

        int userID = validateUser(myLogin.UserName, myLogin.Password);
        if (userID > 0)
        {
            Session["userId"] = userID;
            Response.Redirect("Advertise.aspx",false);
        }
    }

    protected int validateUser(string userName, string password)
    {

        SqlConnection dbcon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connString"].ToString());
        string cmdText="SELECT userID, UserName, UserPass FROM UserLogin WHERE UserName = '"+userName+"' AND UserPass = '"+password+"'";
        dbcon.Open();
        SqlCommand cmd = new SqlCommand(cmdText,dbcon);
        SqlDataReader dr = cmd.ExecuteReader();
        int userID = 0;
        if (dr.Read())
        {
            userID = Convert.ToInt32(dr["userID"]);
        }
        dr.Dispose();
        dbcon.Close();
        return userID;
    }
}


I hope you will like the post, for appreciation and comment you are most welcome..

Cheers,
ved pathak

Comments