locked
Custom Login system! RRS feed

  • Question

  • User1114182319 posted

     Hi i am trying to get my login system up and running but am not sure how. I have the main web page to my site that asks the user for a username and password, and these are entered into the textfields UserBox and PassBox, but i am not sure how to check if the information entered in the fields machs any data that currently reside in my database and if it does i want to take them to the next page. I am working from an access data base and i have the following code place within my web.config file and i would really appreciate some help on this? thanks

     

       <connectionStrings>

        <add name="Users" connectionString="Data Source=user.mdb; Integrated Security=SSPI;" providerName="System.Data.OleDb"/>

      </connectionStrings>

    Friday, February 13, 2009 2:03 PM

Answers

  • User-821857111 posted

       <connectionStrings>

        <add name="Users" connectionString="Data Source=user.mdb; Integrated Security=SSPI;" providerName="System.Data.OleDb"/>

      </connectionStrings>
     

    That's a mix-mash of Access and SQL Server connection strings.

    Here's a simple login and redirect for Access

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 13, 2009 5:16 PM

All replies

  • User-1199946673 posted

    Put the database in the App_Data folder, and then change the connectionstring to: 

    <connectionStrings>
      <add name="Users"
      	connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|user.mdb"
       	providerName="System.Data.OleDb" />
    </connectionStrings>

    http://www.mikesdotnetting.com/Article.aspx?ArticleID=78

    And you can off course use your own login system, but what is wrong with the build in Membership providers? Here you can find how to use them with access

    Friday, February 13, 2009 3:47 PM
  • User-821857111 posted

       <connectionStrings>

        <add name="Users" connectionString="Data Source=user.mdb; Integrated Security=SSPI;" providerName="System.Data.OleDb"/>

      </connectionStrings>
     

    That's a mix-mash of Access and SQL Server connection strings.

    Here's a simple login and redirect for Access

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 13, 2009 5:16 PM
  • User1114182319 posted

     I have now changed my code and added the following:

     string connect = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=|DataDirectory|user.mdb";
            string query = "Select Count(*) From users Where Username = ? And UserPass = ?";
            int result = 0;
            using (OleDbConnection conn = new OleDbConnection(connect))
            {
                using (OleDbCommand cmd = new OleDbCommand(query, conn))
                {
                    cmd.Parameters.AddWithValue("", UsernameBox.Text);
                    cmd.Parameters.AddWithValue("", PasswordBox.Text);
                    conn.Open();
                    Session["User"] = UsernameBox.Text;
                    result = (int)cmd.ExecuteScalar();
                }
            }
            if (result > 0)
            {
                Response.Redirect("userinfo.aspx");
            }
            else
            {
                wrongdetails.Text = "Invalid credentials!!";
            }

     

    I run this code however i get the following build errors:

    Error    1    The name 'UsernameBox' does not exist in the current context    C:\Users\Jack O'Neill\Documents\Visual Studio 2008\WebSites\UserManagement\Default.aspx.cs    35    49    C:\...\UserManagement\
    Error    2    The name 'PasswordBox' does not exist in the current context    C:\Users\Jack O'Neill\Documents\Visual Studio 2008\WebSites\UserManagement\Default.aspx.cs    36    49    C:\...\UserManagement\
    Error    3    The name 'wrongdetails' does not exist in the current context    C:\Users\Jack O'Neill\Documents\Visual Studio 2008\WebSites\UserManagement\Default.aspx.cs    48    13    C:\...\UserManagement\


    I proceeded with the build even tho there were errors and the wrongdetails text field was altered when i entered a username and pass that didnt exist, however when i enter a user and pass that did exist i got the following compile errors which i believe to be related to the build errors that i skipped, can any1 help with this?

     

     

    Compilation Error

    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

    Compiler Error Message: CS0103: The name 'UsernameBox' does not exist in the current context

    Source Error:


    Line 33:             using (OleDbCommand cmd = new OleDbCommand(query, conn))
    Line 34: {
    Line 35: cmd.Parameters.AddWithValue("", UsernameBox.Text);
    Line 36: cmd.Parameters.AddWithValue("", PasswordBox.Text);
    Line 37: conn.Open();

    Saturday, February 14, 2009 1:14 PM
  • User-821857111 posted

    That means that the TextBoxes in the aspx file cannot be seen in the .cs file.  Are you sure that you have the right IDs?  And where did you put the code?


    Saturday, February 14, 2009 1:46 PM
  • User1114182319 posted

     yes i have checked the ID's and they are definatly right and the code is placed on the submit button click event.

    i cant understand why it says they cant be found because wen the wrong details are entered the text box changes so it obviously it is right and the code doesnt have any red underlining when i write it but when i compile it the code for the text fields does have blue underlining im not sure why?

    Saturday, February 14, 2009 2:01 PM
  • User-1199946673 posted

    Is it possible that the textboxes are located inside another control, for example inside a formview?

    Saturday, February 14, 2009 6:23 PM
  • User1114182319 posted

     Ye i have checked and i dont think they are located within another function, below i have places the main page source code and my code behind file code:

    <%@ 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>
        <style type="text/css" media="all">
            @import "StyleSheet.css";
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
        <div id="container">
       
        <div id="mainContent">
       
        <div id="loginDetails">
           
       
            <asp:Label ID="UserLabel" runat="server" Text="Username:"></asp:Label>
    &nbsp;
            <asp:TextBox ID="UsernameBox" runat="server"></asp:TextBox>
            <br />
            <br />
            <asp:Label ID="PasswordLabel" runat="server" Text="Password:"></asp:Label>
    &nbsp;
            <asp:TextBox ID="PasswordBox" runat="server" TextMode="Password"></asp:TextBox>
            <br />
            <br />
            <asp:Button ID="SubmitBttn" runat="server" onclick="SubmitBttn_Click"
                Text="Submit" />
    &nbsp;
            <asp:Button ID="Button2" runat="server" Text="Register" />
            <br />
            <asp:Label ID="wrongdetails" runat="server"></asp:Label>
       
       
       
        </div>   
       
           
       
        </div>
       
        <asp:Label ID="CopyrightLabel" runat="server" Text=""></asp:Label>
       
        </div>
        </form>
    </body>
    </html>

     

    CODE BEHIND FILE:

    using System;
    using System.Data;
    using System.Data.OleDb;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Configuration;
    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)
        {
            if (!IsPostBack)
            {
                string copyright = ConfigurationManager.AppSettings["Copyright"];//This retrieves the value of the copyright information from the config file

                CopyrightLabel.Text = copyright;//This changes the text of the label to match the text of the coyright variable
           
               
            }

        }
        protected void SubmitBttn_Click(object sender, EventArgs e)
        {

            string connect = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=|DataDirectory|user.mdb";
            string query = "Select Count(*) From users Where Username = ? And UserPass = ?";
            int result = 0;
            using (OleDbConnection conn = new OleDbConnection(connect))
            {
                using (OleDbCommand cmd = new OleDbCommand(query, conn))
                {
                    cmd.Parameters.AddWithValue("", UsernameBox.Text);
                    cmd.Parameters.AddWithValue("", PasswordBox.Text);
                    conn.Open();
                    Session["User"] = UsernameBox.Text;
                    result = (int)cmd.ExecuteScalar();
                }
            }
            if (result > 0)
            {
                Response.Redirect("userinfo.aspx");
            }
            else
            {
                wrongdetails.Text = "Invalid Username or Password!!";
            }

        }
    }
     

    Sunday, February 15, 2009 9:53 AM
  • User-821857111 posted

    I just copied and pasted your code as-is, and it works fine.

     

    Sunday, February 15, 2009 12:04 PM
  • User1114182319 posted

     i cant understand why it is not working on mine then, i am using visual studio 2008 is that ok? this is annoying because there is no logical explanation for it

    Sunday, February 15, 2009 3:10 PM
  • User-821857111 posted

    Create a new page, copy and paste the code and try it again.

     

    Sunday, February 15, 2009 3:28 PM
  • User1114182319 posted

     issue solved thanks its my own stupid fault i didnt have the inherits set up properly on the other pages, thanks for all ya help!

    Sunday, February 15, 2009 6:14 PM