Tuesday, April 17, 2012

Login Example

Don't forget to add this line to the web.config file. Also don't break up the connectionString line. I did that to make it fit on the blog page.


<connectionStrings>
     <add name="CommunityAssistConnection" 
          connectionString="Data Source=LocalHost;
              initial catalog=CommunityAssist;
              integrated security=true" />
</connectionStrings>

Here is the login class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
//the Ado and config libraries
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

/// 
/// Summary description for LoginClass
/// 
public class LoginClass
{
    private string user;
    private string pass;
 
        public LoginClass(string username, string password)
        {
            user=username;
            pass=password;
        }

        public bool ValidateLogin()
        {
            bool valid = false;
            SqlConnection connect = 
                new SqlConnection
                    (ConfigurationManager.
                    ConnectionStrings["CommunityAssistConnection"].
                    ToString());

            string Sql = "SELECT LastName, SSNumber "
                            + "FROM Person "
                            + "Inner Join Employee "
                            + "On Person.PersonKey=Employee.PersonKey "
                            + " Where Lastname=@user "
                            + "And SSNumber=@pass";

            SqlCommand cmd = new SqlCommand(Sql, connect);
            cmd.Parameters.AddWithValue("@user", user);
            cmd.Parameters.AddWithValue("@pass", pass);

            DataSet ds = new DataSet();

            SqlDataReader reader = null;
            connect.Open();
            reader = cmd.ExecuteReader();
            ds.Load(reader, System.Data.LoadOption.OverwriteChanges,"Validate");
            reader.Dispose();
            connect.Close();

            foreach (DataRow row in ds.Tables["Validate"].Rows)
            {
                if (row["LastName"].ToString().Equals(user) 
&& row["SSNumber"].ToString().Equals(pass))
                {
                    valid = true;
                    
                }
            }




            return valid;
        }
}

here is source for Default.aspx

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

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Login ID="Login1" runat="server" BackColor="#F7F7DE" BorderColor="#CCCC99" 
            BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" 
            Font-Size="10pt" onauthenticate="Login1_Authenticate1">
            <TitleTextStyle BackColor="#6B696B" Font-Bold="True" ForeColor="#FFFFFF"  />
        </asp:Login>
    </div>
    </form>
</body>
</html>

Here is the code behind Default.aspx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    
    protected void Login1_Authenticate1(object sender, AuthenticateEventArgs e)
    {
        LoginClass l = new LoginClass(Login1.UserName, Login1.Password);
        bool good = l.ValidateLogin();
        if (good)
        {
            e.Authenticated = true;
            Session["loginName"] = Login1.UserName.ToString();
            Response.Redirect("Default2.aspx");

        }
    }
}

Here is the source for Default2.aspx

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

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>



Here is the code behind Default2.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string name = Session["loginName"].ToString();
        Label1.Text = "Welcome " + name;
    }
}

No comments:

Post a Comment