C# .NET - Set a user as logged in programatically

Asked By Dom Afonso on 28-Sep-12 05:01 AM
I have created a methods that creates a user in sql without using asp.net membership methods. Since I used sha256 to hash the password, I cannot use the membership validate method to authenticate the user. What I do is to hash the user's password, select hashed password from the database and compare. And, if they are equal, then I redirect the user to main user's page. But, it returns a log in page in web.conf. Can anyone let me know what c# code shall I use so that when the compared values are iqual the user is redirected to appropriete page instead of log in one?  
Jitendra Faye replied to Dom Afonso on 01-Oct-12 01:47 AM

If you wan to authenticate any user then first you have to check that given Username and password is correct or not.

you can do this by using this code-


protected


void btnLogin_Click(object sender, EventArgs e)

{

if

(TxtUserName.Text != "" && TxtPassword.Text != "")

{

SqlConnection con =

new SqlConnection("con string");

SqlDataAdapter da;

string

mySQL = "SELECT * FROM logintable where username='" + TxtUserName.Text + "' and password='" + TxtPassword.Text + "'";

da = new SqlDataAdapter(mySQL, con);

con.Open();

DataSet ds =

new DataSet();

da.Fill(ds);

if

(ds.Tables[0].Rows.Count > 0)

{

Response.Redirect(

"home.aspx");

}

else

{

Response.Write(

"<script>alert('Invalid Username or Password.')</script>");

}

}


USE THIS CODE AND LET ME KNOW.