Click here to Skip to main content
15,904,416 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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.Xml.Linq;
using System.Data.SqlClient;

public partial class Login : System.Web.UI.Page
{
    SqlConnection  con; 
    SqlCommand  cmd;
    SqlDataReader dr;
    
    protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {
        SqlConnection con = new SqlConnection(MyClass.GetConnection());
        SqlCommand cm = new SqlCommand("Select * from AllUsers where userid=@userid and psw=@psw", con);
        SqlParameter p1 = cm.Parameters.Add("userid", SqlDbType.VarChar, 50);
        SqlParameter p2 = cm.Parameters.Add("psw", SqlDbType.VarChar, 50);
        p1.Value = Login1.UserName;
        p2.Value = Login1.Password;
        if (con.State == ConnectionState.Closed) 
            con.Open();
        SqlDataReader dr = cm.ExecuteReader();
        if (dr.Read())
        {
            
            Session.Add("userid", Login1.UserName);
            Session.Add("fname", dr["fname"]);
            Session.Add("Hospital_MinicipalityId", dr["Hospital_MinicipalityId"]);
            if (dr["Role"].ToString().Equals("H"))
            {
                con.Close();
                Response.Redirect("HospitalHome.aspx");
            }
            else if (dr["Role"].ToString().Equals("M"))
            {
                con.Close();
                Response.Redirect("MunicipalityHomePage.aspx");
            }
            else if (dr["Role"].ToString().Equals("A"))
            {
                con.Close();
                Response.Redirect("AdminHomePage.aspx");
            }
            else
            {
                con.Close();
                Response.Redirect("CDMOHomePage.aspx");
            }
        }
        Login1.FailureText = "Invalid User id /Password Login Denied";
        con.Close();
Posted
Updated 1-May-11 5:40am
v3
Comments
DaveAuld 1-May-11 11:41am    
Edit, added code formatting and removed 'its urgent', because you will only get flamed for putting that. Its only urgent to you and not everybody else on CP!
Sandeep Mewara 1-May-11 12:23pm    
You asking us to give Unit-Test code for this login code of yours?
jim lahey 2-May-11 7:55am    
You're doing it wrong. You should have written the test first.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900