Authentication

G

Guest

Can someone tell me what is wrong with this code? When I test it it does
nothing but return to the log in page. As far as I can see the web.config is
working fine and the authentication element is without any problem. I
think it could be a problem with the cookie ticket but I don't know how I can
confirm. Any suggestions would be greatly appreciated.

Thank you

Sam-
public class Login : System.Web.UI.Page
{
protected System.Data.SqlClient.SqlDataAdapter tqda1;
protected System.Data.SqlClient.SqlDataAdapter tqcda1;
protected System.Web.UI.WebControls.TextBox u_username;
protected System.Web.UI.WebControls.Repeater tqrep;
protected System.Data.DataSet tqDS1;
protected System.Web.UI.WebControls.TextBox u_password;
protected System.Web.UI.WebControls.RequiredFieldValidator
RequiredFieldValidator1;
protected System.Web.UI.WebControls.RequiredFieldValidator
RequiredFieldValidator2;
protected System.Web.UI.WebControls.ImageButton Imagebutton1;
protected System.Web.UI.WebControls.Label lblMessage;
protected System.Data.SqlClient.SqlConnection tqconn1;


void Button_Click(object s, System.EventArgs e)
{
if(IsValid)
{
if(DBAuthenticate(u_username.Text, u_password.Text)>0)
{
FormsAuthentication.RedirectFromLoginPage(u_username.Text, false);
}
}
}
int DBAuthenticate( string strUsername, string strPassword )
{
SqlConnection tq_sec_conn;
SqlCommand tq_sec_cmd;
SqlParameter tq_sec_param;
int intResult;

string tqConSec="workstation id=IWAVE.tqsqldb.dbo;packet
size=4096;integrated security=SSPI;data source=IWAVE;persist security
info=False;initial catalog=tqsqldb";

tq_sec_conn=new SqlConnection(tqConSec);
tq_sec_cmd=new SqlCommand("tqsecure",tq_sec_conn);
tq_sec_cmd.CommandType=CommandType.StoredProcedure;
tq_sec_param=tq_sec_cmd.Parameters.Add("RETURN_VALUE", SqlDbType.Int);
tq_sec_param.Direction=ParameterDirection.ReturnValue;
tq_sec_cmd.Parameters.Add("@username", strUsername);
tq_sec_cmd.Parameters.Add("@password", strPassword);
tq_sec_conn.Open();
tq_sec_cmd.ExecuteNonQuery()
intResult=System.Convert.ToInt32(tq_sec_cmd.Parameters["RETURN_VALUE"].Value);
tq_sec_conn.Close();

if ( intResult < 0 )
{
if ( intResult == -1 )
{
lblMessage.Text = "Username ! Registered!";
}
else
{
lblMessage.Text = "Invalid Password!";
}
}
return intResult;
}
 
M

MattC

Try

if(DBAuthenticate(u_username.Text, u_password.Text)>0)
{
FormsAuthenticationTicket authTicket = new
FormsAuthenticationTicket(1,"loggedonuser", DateTime.Now,
DateTime.Now.AddMinutes(Session.Timeout),falseu_username.Text);

//Encrypt the ticket.
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);

//Create a cookie, and then add the encrypted ticket to the cookie as
data.
HttpCookie authCookie = new
HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);

//Add the cookie to the outgoing cookies collection.
Response.Cookies.Add(authCookie);

//You can redirect now.
FormsAuthentication.RedirectFromLoginPage(u_username.Text, false);
}

I am Sam said:
Can someone tell me what is wrong with this code? When I test it it does
nothing but return to the log in page. As far as I can see the web.config is
working fine and the authentication element is without any problem. I
think it could be a problem with the cookie ticket but I don't know how I can
confirm. Any suggestions would be greatly appreciated.

Thank you

Sam-
public class Login : System.Web.UI.Page
{
protected System.Data.SqlClient.SqlDataAdapter tqda1;
protected System.Data.SqlClient.SqlDataAdapter tqcda1;
protected System.Web.UI.WebControls.TextBox u_username;
protected System.Web.UI.WebControls.Repeater tqrep;
protected System.Data.DataSet tqDS1;
protected System.Web.UI.WebControls.TextBox u_password;
protected System.Web.UI.WebControls.RequiredFieldValidator
RequiredFieldValidator1;
protected System.Web.UI.WebControls.RequiredFieldValidator
RequiredFieldValidator2;
protected System.Web.UI.WebControls.ImageButton Imagebutton1;
protected System.Web.UI.WebControls.Label lblMessage;
protected System.Data.SqlClient.SqlConnection tqconn1;


void Button_Click(object s, System.EventArgs e)
{
if(IsValid)
{
if(DBAuthenticate(u_username.Text, u_password.Text)>0)
{
FormsAuthentication.RedirectFromLoginPage(u_username.Text, false);
}
}
}
int DBAuthenticate( string strUsername, string strPassword )
{
SqlConnection tq_sec_conn;
SqlCommand tq_sec_cmd;
SqlParameter tq_sec_param;
int intResult;

string tqConSec="workstation id=IWAVE.tqsqldb.dbo;packet
size=4096;integrated security=SSPI;data source=IWAVE;persist security
info=False;initial catalog=tqsqldb";

tq_sec_conn=new SqlConnection(tqConSec);
tq_sec_cmd=new SqlCommand("tqsecure",tq_sec_conn);
tq_sec_cmd.CommandType=CommandType.StoredProcedure;
tq_sec_param=tq_sec_cmd.Parameters.Add("RETURN_VALUE", SqlDbType.Int);
tq_sec_param.Direction=ParameterDirection.ReturnValue;
tq_sec_cmd.Parameters.Add("@username", strUsername);
tq_sec_cmd.Parameters.Add("@password", strPassword);
tq_sec_conn.Open();
tq_sec_cmd.ExecuteNonQuery();
intResult=System.Convert.ToInt32(tq_sec_cmd.Parameters["RETURN_VALUE"].Value
);
tq_sec_conn.Close();

if ( intResult < 0 )
{
if ( intResult == -1 )
{
lblMessage.Text = "Username ! Registered!";
}
else
{
lblMessage.Text = "Invalid Password!";
}
}
return intResult;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,173
Latest member
GeraldReund
Top