Simple Authentication
-
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!! -
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!! -
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!!mil_an wrote: 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 You did't say what the error was. You said WHERE it was, but not WHAT... RageInTheMachine9532
-
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!! -
Hi, Sorry, this is the error message: System.NullReferenceException: Object reference not set to an instance of an object. Thanks,
-
Hi, Sorry, this is the error message: System.NullReferenceException: Object reference not set to an instance of an object. Thanks,
It just occured to me that your using 'this' in front of everything. You don't need to... RageInTheMachine9532
-
It just occured to me that your using 'this' in front of everything. You don't need to... RageInTheMachine9532
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 -
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
-
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
-
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
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
-
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
-
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!!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
-
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 HoaglandMazdak wrote: you can use if you want. That's what I mean...it's just a bunch of extra typing for nothing... RageInTheMachine9532