S
Steve
I can not get forms authentication to work. Here is my situation.
I created a test website with two file:
login.aspx
default.aspx
default.aspx is set as the default web page.
I type in the URL http://localhost/test
login.aspx comes up.
I enter my user name and password, but instead of authenticating me and taking me to the default.aspx page, the login.aspx screen reappears.
Everything that I have read about this says that what I have in my login.aspx and web.config files is all that I need. Yet I can't understand why this is happening.
Other random facts: cookies are enabled on the computer.
Here are my files:
************************************
web.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation
defaultLanguage="c#"
debug="true"
/>
<customErrors
mode="RemoteOnly"
/>
<authentication mode="Forms">
<forms name=".test"
loginUrl = "login.aspx"
protection = "All"
path = "/"
timeout = "30"
/>
</authentication>
<authorization>
<deny users="?" />
</authorization>
<trace
enabled="false"
requestLimit="10"
pageOutput="false"
traceMode="SortByTime"
localOnly="true"
/>
<sessionState
mode="InProc"
stateConnectionString="tcpip=127.0.0.1:42424"
sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes"
cookieless="false"
timeout="20"
/>
<globalization
requestEncoding="utf-8"
responseEncoding="utf-8"
/>
</system.web>
</configuration>
************************************
login.aspx
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Security;
using System.Configuration;
namespace test
{
/// <summary>
/// Summary description for LoginForm.
/// </summary>
public class LoginForm : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox txtUsername;
protected System.Web.UI.WebControls.RequiredFieldValidator rfvUsername;
protected System.Web.UI.WebControls.Button btnLogin;
protected System.Web.UI.WebControls.Label lblPWD;
protected System.Web.UI.WebControls.TextBox txtPWD;
protected System.Web.UI.WebControls.RequiredFieldValidator rfvPWD;
protected System.Web.UI.WebControls.PlaceHolder phErrorMsg;
protected System.Data.OleDb.OleDbConnection conLogin;
protected System.Data.OleDb.OleDbCommand cmdLogin;
protected System.Web.UI.WebControls.Label lblUserName;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.conLogin = new System.Data.OleDb.OleDbConnection();
this.cmdLogin = new System.Data.OleDb.OleDbCommand();
this.btnLogin.Click += new System.EventHandler(this.btnLogin_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnLogin_Click(object sender, System.EventArgs e)
{
Control ctlControl;
OleDbDataReader dtrLogin;
string sConn;
string sLoginQuery;
sConn = "Provider=" + Constants.Provider +
";Data Source=" + Constants.DATASource;
sLoginQuery = "SELECT tblUser.* " +
"FROM tblUser " +
"WHERE ((tblUser.Logon=@Username) AND (tblUser.Password=@PWD))";
if (IsValid)
{
// get user login info
// open up the connection
conLogin = new OleDbConnection(sConn);
conLogin.Open();
// construct the query to extract login
cmdLogin = new OleDbCommand(sLoginQuery, conLogin);
cmdLogin.Parameters.Add("@username", txtUsername.Text);
cmdLogin.Parameters.Add("@PWD", txtPWD.Text);
dtrLogin = cmdLogin.ExecuteReader();
if (dtrLogin.HasRows)
{
// if returns rows (should only return one) then valid login
// get the user id and redirect to get a plan id
dtrLogin.Read();
Session["UserID"] = Convert.ToInt32(dtrLogin["UserID"]);
Session["ScreenName"] = (dtrLogin["User"]).ToString();
conLogin.Close();
FormsAuthentication.RedirectFromLoginPage(Convert.ToString(Session["UserID"]), false);
}
else
{
// not a valid login. display error
conLogin.Close();
ctlControl = LoadControl("InvalidLogin.ascx");
phErrorMsg.Controls.Add(ctlControl);
}
}
}
}
}
***********************************
I created a test website with two file:
login.aspx
default.aspx
default.aspx is set as the default web page.
I type in the URL http://localhost/test
login.aspx comes up.
I enter my user name and password, but instead of authenticating me and taking me to the default.aspx page, the login.aspx screen reappears.
Everything that I have read about this says that what I have in my login.aspx and web.config files is all that I need. Yet I can't understand why this is happening.
Other random facts: cookies are enabled on the computer.
Here are my files:
************************************
web.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation
defaultLanguage="c#"
debug="true"
/>
<customErrors
mode="RemoteOnly"
/>
<authentication mode="Forms">
<forms name=".test"
loginUrl = "login.aspx"
protection = "All"
path = "/"
timeout = "30"
/>
</authentication>
<authorization>
<deny users="?" />
</authorization>
<trace
enabled="false"
requestLimit="10"
pageOutput="false"
traceMode="SortByTime"
localOnly="true"
/>
<sessionState
mode="InProc"
stateConnectionString="tcpip=127.0.0.1:42424"
sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes"
cookieless="false"
timeout="20"
/>
<globalization
requestEncoding="utf-8"
responseEncoding="utf-8"
/>
</system.web>
</configuration>
************************************
login.aspx
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Security;
using System.Configuration;
namespace test
{
/// <summary>
/// Summary description for LoginForm.
/// </summary>
public class LoginForm : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox txtUsername;
protected System.Web.UI.WebControls.RequiredFieldValidator rfvUsername;
protected System.Web.UI.WebControls.Button btnLogin;
protected System.Web.UI.WebControls.Label lblPWD;
protected System.Web.UI.WebControls.TextBox txtPWD;
protected System.Web.UI.WebControls.RequiredFieldValidator rfvPWD;
protected System.Web.UI.WebControls.PlaceHolder phErrorMsg;
protected System.Data.OleDb.OleDbConnection conLogin;
protected System.Data.OleDb.OleDbCommand cmdLogin;
protected System.Web.UI.WebControls.Label lblUserName;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.conLogin = new System.Data.OleDb.OleDbConnection();
this.cmdLogin = new System.Data.OleDb.OleDbCommand();
this.btnLogin.Click += new System.EventHandler(this.btnLogin_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnLogin_Click(object sender, System.EventArgs e)
{
Control ctlControl;
OleDbDataReader dtrLogin;
string sConn;
string sLoginQuery;
sConn = "Provider=" + Constants.Provider +
";Data Source=" + Constants.DATASource;
sLoginQuery = "SELECT tblUser.* " +
"FROM tblUser " +
"WHERE ((tblUser.Logon=@Username) AND (tblUser.Password=@PWD))";
if (IsValid)
{
// get user login info
// open up the connection
conLogin = new OleDbConnection(sConn);
conLogin.Open();
// construct the query to extract login
cmdLogin = new OleDbCommand(sLoginQuery, conLogin);
cmdLogin.Parameters.Add("@username", txtUsername.Text);
cmdLogin.Parameters.Add("@PWD", txtPWD.Text);
dtrLogin = cmdLogin.ExecuteReader();
if (dtrLogin.HasRows)
{
// if returns rows (should only return one) then valid login
// get the user id and redirect to get a plan id
dtrLogin.Read();
Session["UserID"] = Convert.ToInt32(dtrLogin["UserID"]);
Session["ScreenName"] = (dtrLogin["User"]).ToString();
conLogin.Close();
FormsAuthentication.RedirectFromLoginPage(Convert.ToString(Session["UserID"]), false);
}
else
{
// not a valid login. display error
conLogin.Close();
ctlControl = LoadControl("InvalidLogin.ascx");
phErrorMsg.Controls.Add(ctlControl);
}
}
}
}
}
***********************************