locked
ASP.NET login control with MySql RRS feed

  • Question

  • User-1451598595 posted

    Hello,

    I have got another problem.. i want to connect my login control with specyfic table in database which contains information such as login and password. I do it this way:

    protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
        {
            Boolean blnresult;
            blnresult = false;
    
            // Pass UserName  and Password from login1 control to an authentication function which will check will check the user name and password from sql server.
            // Then will retrun a true or false value into blnresult variable
            blnresult = Authentication(loginField.UserName, loginField.Password);
    
            // If blnresult has a true value then authenticate user 
            if (blnresult == true)
            {
                // This is the actual statement which will authenticate the user
                e.Authenticated = true;
                // Store your authentication mode in session variable 
                Session["Check"] = true;
                Response.Redirect("contactlist.aspx");
    
            }
            else
                // If user faild to provide valid user name and password
                e.Authenticated = false;
        }
        protected static Boolean Authentication(string login, string password)
        {
            string sqlstring;
            MySqlConnection connection = new MySqlConnection("server=localhost;database=contextv4;uid=root;password=siewer");
            sqlstring = "Select login, password from user where login='" + login + "' and password ='" + password + "'";
    
            // create a sql command which will user connection string and your select statement string 
            MySql.Data.MySqlClient.MySqlCommand comm = new MySql.Data.MySqlClient.MySqlCommand(sqlstring, connection);
    
            // create a sqldatabase reader which will execute the above command to get the values from sqldatabase
            MySql.Data.MySqlClient.MySqlDataReader reader;
    
            // open a connection with sqldatabase
            connection.Open();
    
            // execute sql command and store a return values in reade
            reader = comm.ExecuteReader();
    
    
            // check if reader hase any value then return true otherwise return false
            if (reader.Read())
            {
                connection.Close();
    
                return true;
            }
            else
            {
                connection.Close();
                return false;
            }
        }

    And it works.

    Problem is that, when i start my login website and i write url of another site i can enter this site without need of being logged it. All i want to do is to require being logged in to enter other sites ( while entering url - redirect to login). Can anyone help me?

    GrzegorzS.

    Friday, August 24, 2012 6:16 AM

Answers

  • User-578610739 posted

    Hi Grzegorz S.

    You may use cookie for this. at the time of login you create cookie with expired time. Now at the new browser every time , first check in cookie , it was expired or not. If yes , then sent to first page of your application rather than login.

    http://forums.asp.net/t/1425148.aspx/1

    http://timmaxey.net/archive/2009/03/06/asp.net-cookie-auto-log-in.aspx

    http://www.codeproject.com/Articles/244904/Cookies-in-ASP-NET

    http://www.codeproject.com/Articles/11069/all-thing-about-cookies-in-asp-net-c

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 24, 2012 6:50 AM

All replies

  • User-578610739 posted

    Hi Grzegorz S.

    You may use cookie for this. at the time of login you create cookie with expired time. Now at the new browser every time , first check in cookie , it was expired or not. If yes , then sent to first page of your application rather than login.

    http://forums.asp.net/t/1425148.aspx/1

    http://timmaxey.net/archive/2009/03/06/asp.net-cookie-auto-log-in.aspx

    http://www.codeproject.com/Articles/244904/Cookies-in-ASP-NET

    http://www.codeproject.com/Articles/11069/all-thing-about-cookies-in-asp-net-c

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 24, 2012 6:50 AM
  • User-1451598595 posted

    Ajay,

    Your post was extremly helpfull, Thank you! 

    GrzegorzS.

    Wednesday, August 29, 2012 4:29 AM