We help IT Professionals succeed at work.
Get Started

ASP.NET 3.5 login page (C#)

haramsde
haramsde asked
on
15,048 Views
Last Modified: 2013-12-17
Hi experts

I'm trying to create a login page that will authenticate users that are in a SQL Server 2005 database. I'm creating the login page in Visual Studio 2008 Professional Edition (Trial Version).

The login page is fine, it's the class that is authenticating the user that I'm having problems with. It has given me errors with my case statement amongst other things and I'm not sure how to correct it.
Any help would be greatly appreciated.

Thanks
Login page:
 
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="SmartLearner.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>Secure Site</title>    
    <meta content="Microsoft Visual Studio 7.0" name="GENERATOR"/>    
    <meta content="C#" name="CODE_LANGUAGE"/>    
    <meta content="JavaScript" name="vs_defaultClientScript"/>    
    <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema"/>  
    </head>  
    <body>    
    <form id="login" method="post" runat="server">      
    <table cellspacing="0" cellpadding="0" border="0">      
    <tr>        
    <td valign="top" align="left">          
    <asp:label id="Message" runat="server" ForeColor="#ff0000">          
    </asp:label>        
    </td>      
    </tr>      
    <tr>        
    <td valign="top" align="left">          
    <b>E-mail:</b>        
    </td>      
    </tr>      
    <tr>        
    <td valign="top" align="left">          
    <asp:textbox id="username" runat="server" Width="120">          
    </asp:textbox>        
    </td>      
    </tr>      
    <tr>        
    <td valign="top" align="left">          
    <b>Password:</b>        
    </td>      
    </tr>      
    <tr>        
    <td valign="top" align="left">          
    <asp:textbox id="password" runat="server" Width="120" textMode="Password">          
    </asp:textbox>        
    </td>      
    </tr>      
    <tr>        
    <td valign="top" align="left">          
    <asp:checkbox id="saveLogin" runat="server" text="<b>Save my login</b>">          
    </asp:checkbox>        
    </td>      
    </tr>      
    <tr>        
    <td valign="top" align="right">          
        &nbsp;</td>      
    </tr>      
    </table>    
        <asp:Button ID="btnLogin" runat="server" text="Login" />
    </form>  
    </body>
</html>
 
Class:
 
using System;
using System.Data;
using System.Configuration;
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;
 
namespace SmartLearner
{
    public class CCommonDB: CSql  {public CCommonDB() : base() { }    
        public string AuthenticateUser(      
            System.Web.SessionState.HttpSessionState objSession, // Session Variable      
            System.Web.HttpResponse objResponse,                 // Response Variable      
            string email,                                        // Login      
            string password,                                     // Password      
            bool bPersist                                        // Persist login      
            )    
        {      
            int nLoginID  = 0;      
            int nLoginType  = 0;      // Log the user in      
            Login(email, password, ref nLoginID, ref nLoginType);      
            if(nLoginID != 0)  // Success      
            {        // Log the user in        
                System.Web.Security.FormsAuthentication.SetAuthCookie(nLoginID.ToString(), bPersist);        
                // Set the session varaibles            
                objSession["loginID"]  = nLoginID.ToString();        
                objSession["loginType"] = nLoginType.ToString();        
                // Set cookie information incase they made it persistant        
                System.Web.HttpCookie wrapperCookie = new System.Web.HttpCookie("wrapper");        
                wrapperCookie.Value = objSession["wrapper"].ToString();        
                wrapperCookie.Expires = DateTime.Now.AddDays(30);              
                System.Web.HttpCookie lgnTypeCookie = new System.Web.HttpCookie("loginType");        
                lgnTypeCookie.Value = objSession["loginType"].ToString();        
                lgnTypeCookie.Expires = DateTime.Now.AddDays(30);        
                // Add the cookie to the response        
                objResponse.Cookies.Add(wrapperCookie);        
                objResponse.Cookies.Add(lgnTypeCookie);        
                return "/default.aspx";          
            }          
            case 1:  // Admin Login          
            {            
                return "/Admin.aspx";          
            }          
            case 2:  // Staff Login          
            {            
                return "/Staff.aspx";          
            }          
            default:          
            {            
                return string.Empty;          
            }        
        }      
    }      
    else      
{        
    return string.Empty;      
}    
}    
/// <summary>    
/// Verifies the login and password that were given    
/// </summary>    
/// <param name="email">the login</param>    
/// <param name="password">the password</param>    
/// <param name="nLoginID">returns the login id</param>    
/// <param name="nLoginType">returns the login type</param>    
 
    public void Login(string email, string password, ref int nLoginID, ref int nLoginType)    
{      ResetSql();      
DataSet ds = new DataSet();      
// Set our parameters      
SqlParameter paramLogin = new 
SqlParameter("@username", SqlDbType.VarChar, 100);      paramLogin.Value = email;      SqlParameter paramPassword = new SqlParameter("@password", SqlDbType.VarChar, 20);      paramPassword.Value = password;      
 
Command.CommandType = CommandType.StoredProcedure;      
Command.CommandText = "glbl_Login";      
Command.Parameters.Add(paramLogin);      
Command.Parameters.Add(paramPassword);      
 
Adapter.TableMappings.Add("Table", "Login");      
Adapter.SelectCommand = Command;      
Adapter.Fill(ds);      
if(ds.Tables.Count != 0)      
{        
    DataRow row = ds.Tables[0].Rows[0];        
// Get the login id and the login type        
nLoginID  = Convert.ToInt32(row["Login_ID"].ToString());        
nLoginType  = Convert.ToInt32(row["Login_Type"].ToString());      
}      
else      
{        
nLoginID = 0;        
nLoginType = 0;      
}    
}  
}
}
abstract public class CSql  
{    
private SqlConnection sqlConnection;      // Connection string    
private SqlCommand sqlCommand;          // Command    
private SqlDataAdapter sqlDataAdapter;      // Data Adapter          
private DataSet sqlDataSet;            // Data Set    
public CSql()    
{      
sqlConnection  = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);      
sqlCommand    = new SqlCommand();      
sqlDataAdapter  = new SqlDataAdapter();      
sqlDataSet    = new DataSet();      
sqlCommand.Connection = sqlConnection;    
}    
/// <summary>    
/// Access to our sql command    
/// </summary>    
protected SqlCommand Command    
{      
    get { return sqlCommand; }    }    
/// <summary>    
/// Access to our data adapter    
/// </summary>    
protected SqlDataAdapter Adapter    
{      
    get { return sqlDataAdapter; }    }    
/// <summary>    
/// Makes sure that everything is clear and ready for a new query    
/// </summary>    
 
protected void ResetSql()    
{      
    if(sqlCommand != null)      
{        sqlCommand = new SqlCommand();        
sqlCommand.Connection = sqlConnection;      
}      
    if(sqlDataAdapter != null)        
sqlDataAdapter = new SqlDataAdapter();      
if(sqlDataSet != null)        
sqlDataSet = new DataSet();    }    
 
/// <summary>    
/// Runs our command and returns the dataset    
/// </summary>    
/// <returns>the data set</returns>    
protected DataSet RunQuery()    
{      
sqlDataAdapter.SelectCommand = Command;      
sqlConnection.Open();      
sqlConnection.Close();      
sqlDataAdapter.Fill(sqlDataSet);      
return sqlDataSet;    }  
}
}

Open in new window

Comment
Watch Question
Top Expert 2013
Commented:
This problem has been solved!
Unlock 1 Answer and 8 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE