0

I am trying to implement a Login validation using C# 2005 in ASP.net 2.0 web application. The SQL Server database contains a table named "UserList" with columns LoginId, Password and Role. The Login webform should authenticate the LoginId and password and depending upon the Role assigned to that user/visitor should redirect to a specific webform with a pre-defined
menu options. The role might be Admin, DEO, Accounts or Member. How should I implement it? I have tried the following:

    protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
        {
            try
            {
                string uname = Login1.UserName.Trim(); 
                string password = Login1.Password.Trim(); 

                int flag = AuthenticateUser(uname, password);

                if (flag == 1)
                {
                    e.Authenticated = true;
                    Login1.DestinationPageUrl = "~/MenuAdmin.aspx";
                }
                else if (flag == 2)
                {
                    e.Authenticated = true;
                    Login1.DestinationPageUrl = "~/MenuDEO.aspx";
                }
                else if (flag == 3)
                {
                    e.Authenticated = true;
                    Login1.DestinationPageUrl = "~/MenuAccts.aspx";
                }
                else if (flag == 4)
                {
                    e.Authenticated = true;
                    Login1.DestinationPageUrl = "~/MenuMember.aspx";
                }
                else
                {
                    e.Authenticated = false;
                }
            }

            catch (Exception)
            {
                e.Authenticated = false;
            }
        }

    private int AuthenticateUser(string uname, string password)
        {
            int bflag = 0;
            string connString = ConfigurationManager.ConnectionStrings["LoginDemoConnString"].ConnectionString;
            string strSQL = "Select * FROM UserList where ULoginId ='" + uname + "' AND UPassword ='" + password + "'";

            DataTable dt = new DataTable();
            SqlConnection m_conn;
            SqlDataAdapter m_dataAdapter;

            try
            {
                m_conn = new SqlConnection(connString);
                m_conn.Open();
                m_dataAdapter = new SqlDataAdapter(strSQL, m_conn);
                m_dataAdapter.Fill(dt);
                m_conn.Close();
            }

            catch (Exception ex)
            {
                dt = null;
            }

            finally
            {
                //m_conn.Close();
            }

            if (dt.Rows.Count > 0)
            {
                if (dt.Rows[0][3].ToString() == "Administrator")
                    bflag = 1;
                else if (dt.Rows[0][3].ToString() == "DEO")
                    bflag = 2;
                else if (dt.Rows[0][3].ToString() == "Accts")
                    bflag = 3;
                else
                    bflag = 4;
            }
            return bflag;
        }

Edited by Eager_Beever

2
Contributors
1
Reply
8
Views
4 Years
Discussion Span
Last Post by LastMitch
0

I am trying to implement a Login validation using C# 2005 in ASP.net 2.0 web application. The SQL Server database contains a table named "UserList" with columns LoginId, Password and Role. The Login webform should authenticate the LoginId and password and depending upon the Role assigned to that user/visitor should redirect to a specific webform with a pre-defined

I realized you post this in the wrong section.

This is really more related to ASP.net even though you are using database.

You also need a few more code to make this function.

Read this (Forms Authentication Across Applications):

http://msdn.microsoft.com/en-us/library/eb0zx8fc%28v=vs.100%29.aspx

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.