Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Simple Authentication

Simple Authentication

Scheduled Pinned Locked Moved C#
helpdesignsecurity
13 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M mil_an

    Hi, I am trying to create a very simple authentication form, and am receiving an error. Please help!! This is the code i have used: public class login : System.Web.UI.Page { protected System.Web.UI.WebControls.TextBox txtUsername; protected System.Web.UI.WebControls.TextBox txtPassword; protected System.Web.UI.WebControls.Label lblCorrectLogin; protected System.Web.UI.WebControls.LinkButton LinkButton1; protected System.Web.UI.WebControls.Panel pnlLogIn; protected System.Web.UI.WebControls.Label lblIsAuthenticated; private void Page_Load(object sender, System.EventArgs e) { if (! Page.IsPostBack) { if (Session["isAuthenticated"] == null) { this.lblIsAuthenticated.Text = "null"; } else { this.lblIsAuthenticated.Text = Session["isAuthenticated"].ToString(); } } } private void LinkButton1_Click(object sender, System.EventArgs e) { // Basic User Authentication, might want to replace with your own!!! if (this.txtUsername.Text == "admin" && this.txtPassword.Text == "password") { // Set the Authentication Session Value Session["isAuthenticated"] = true; // Redirect to the Home Page Response.Redirect ("hello.aspx"); } else { // Show error message informing of unmatching UserName and Password pair this.lblCorrectLogin.Text = "Authentification Error!"; } } _______________________ I receive this error: Line 31: if (Session["isAuthenticated"] == null) Line 32: { Line 33: this.lblIsAuthenticated.Text = "null"; Line 34: } Line 35: else Source File: c:\inetpub\wwwroot\authentication\members.aspx.cs Line: 33 _______________________ Thanks for your help!!

    M Offline
    M Offline
    mil_an
    wrote on last edited by
    #4

    Hi, Sorry, this is the error message: System.NullReferenceException: Object reference not set to an instance of an object. Thanks,

    M D 2 Replies Last reply
    0
    • M mil_an

      Hi, Sorry, this is the error message: System.NullReferenceException: Object reference not set to an instance of an object. Thanks,

      M Offline
      M Offline
      Mazdak
      wrote on last edited by
      #5

      As I said you should initialize your label . It may deleteted in your InitializeComponent() method. Mazy "Man is different from animals in that he speculates, a high risk activity." - Edward Hoagland

      M 1 Reply Last reply
      0
      • M mil_an

        Hi, Sorry, this is the error message: System.NullReferenceException: Object reference not set to an instance of an object. Thanks,

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #6

        It just occured to me that your using 'this' in front of everything. You don't need to... RageInTheMachine9532

        M 1 Reply Last reply
        0
        • D Dave Kreskowiak

          It just occured to me that your using 'this' in front of everything. You don't need to... RageInTheMachine9532

          M Offline
          M Offline
          Mazdak
          wrote on last edited by
          #7

          Dave Kreskowiak wrote: It just occured to me that your using 'this' in front of everything. You don't need to... It doesn't change anything. It refer to Page class and controls are added to it, you can use if you want. Mazy "Man is different from animals in that he speculates, a high risk activity." - Edward Hoagland

          D 1 Reply Last reply
          0
          • M Mazdak

            As I said you should initialize your label . It may deleteted in your InitializeComponent() method. Mazy "Man is different from animals in that he speculates, a high risk activity." - Edward Hoagland

            M Offline
            M Offline
            mil_an
            wrote on last edited by
            #8

            Thanks for your help! May I ask how I may initialize the label? Thanks again...:)

            M 1 Reply Last reply
            0
            • M mil_an

              Thanks for your help! May I ask how I may initialize the label? Thanks again...:)

              M Offline
              M Offline
              Mazdak
              wrote on last edited by
              #9

              Something like this:

              Label mylable = new Label();
              this.Controls.Add(mylabel);

              If you use VS.NET when you drag and drop controls in your ASPX file it do all the code for you , but if you don't use it or create your class without it, just create simple web project project , add a label in your webform. Then go to your aspx.cs file and find InitializeComponet() method , all you need is there and you copy/paste same things. The controls and their properties/events are all register their and add to the control. Mazy "Man is different from animals in that he speculates, a high risk activity." - Edward Hoagland

              H 1 Reply Last reply
              0
              • M Mazdak

                Something like this:

                Label mylable = new Label();
                this.Controls.Add(mylabel);

                If you use VS.NET when you drag and drop controls in your ASPX file it do all the code for you , but if you don't use it or create your class without it, just create simple web project project , add a label in your webform. Then go to your aspx.cs file and find InitializeComponet() method , all you need is there and you copy/paste same things. The controls and their properties/events are all register their and add to the control. Mazy "Man is different from animals in that he speculates, a high risk activity." - Edward Hoagland

                H Offline
                H Offline
                Heath Stewart
                wrote on last edited by
                #10

                If the Label is declared in the .aspx file, you don't instantiate it in your code-behind file. The page instantiates it when compiled. Since the code-behind must declare it as protected, this field gets set by the child class (the .aspx page).

                Microsoft MVP, Visual C# My Articles

                M 1 Reply Last reply
                0
                • H Heath Stewart

                  If the Label is declared in the .aspx file, you don't instantiate it in your code-behind file. The page instantiates it when compiled. Since the code-behind must declare it as protected, this field gets set by the child class (the .aspx page).

                  Microsoft MVP, Visual C# My Articles

                  M Offline
                  M Offline
                  Mazdak
                  wrote on last edited by
                  #11

                  Oh, yah. I mixed up with windows application. :rolleyes: Mazy "Man is different from animals in that he speculates, a high risk activity." - Edward Hoagland

                  1 Reply Last reply
                  0
                  • M mil_an

                    Hi, I am trying to create a very simple authentication form, and am receiving an error. Please help!! This is the code i have used: public class login : System.Web.UI.Page { protected System.Web.UI.WebControls.TextBox txtUsername; protected System.Web.UI.WebControls.TextBox txtPassword; protected System.Web.UI.WebControls.Label lblCorrectLogin; protected System.Web.UI.WebControls.LinkButton LinkButton1; protected System.Web.UI.WebControls.Panel pnlLogIn; protected System.Web.UI.WebControls.Label lblIsAuthenticated; private void Page_Load(object sender, System.EventArgs e) { if (! Page.IsPostBack) { if (Session["isAuthenticated"] == null) { this.lblIsAuthenticated.Text = "null"; } else { this.lblIsAuthenticated.Text = Session["isAuthenticated"].ToString(); } } } private void LinkButton1_Click(object sender, System.EventArgs e) { // Basic User Authentication, might want to replace with your own!!! if (this.txtUsername.Text == "admin" && this.txtPassword.Text == "password") { // Set the Authentication Session Value Session["isAuthenticated"] = true; // Redirect to the Home Page Response.Redirect ("hello.aspx"); } else { // Show error message informing of unmatching UserName and Password pair this.lblCorrectLogin.Text = "Authentification Error!"; } } _______________________ I receive this error: Line 31: if (Session["isAuthenticated"] == null) Line 32: { Line 33: this.lblIsAuthenticated.Text = "null"; Line 34: } Line 35: else Source File: c:\inetpub\wwwroot\authentication\members.aspx.cs Line: 33 _______________________ Thanks for your help!!

                    H Offline
                    H Offline
                    Heath Stewart
                    wrote on last edited by
                    #12

                    This is a very inflexible solution. Why not just use forms authentication? It's really easy to use. You simply put an authentication[^] section in your application's root Web.config file. You can add the credentials - even using plain-text passwords - right there in the file and forego having to implement a database or other data source for credentials. Then add an authorization[^] section for each location (either in your root's Web.config or in sub-directories' Web.config files) for directories you want to protect. Then just read about the FormsAuthentication[^] class for a simple example of a page to gather credentials and the code to create the authentication token, which is an encrypted token (read: secure - much more than sessions). This gives you the flexibility to support different data sources for credentials. And even if one day you change the username and/or password, with your approach you'd have to recompile for something so trivial. Even which putting this in the Web.config file you can change it easily without having to do anything else (the change would case the web application to restart but that's transparent). If you want to add role-based authorization, see my article, Role-Based Security with Forms Authentication[^].

                    Microsoft MVP, Visual C# My Articles

                    1 Reply Last reply
                    0
                    • M Mazdak

                      Dave Kreskowiak wrote: It just occured to me that your using 'this' in front of everything. You don't need to... It doesn't change anything. It refer to Page class and controls are added to it, you can use if you want. Mazy "Man is different from animals in that he speculates, a high risk activity." - Edward Hoagland

                      D Offline
                      D Offline
                      Dave Kreskowiak
                      wrote on last edited by
                      #13

                      Mazdak wrote: you can use if you want. That's what I mean...it's just a bunch of extra typing for nothing... RageInTheMachine9532

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups