使用SQl Server 2008 R2 Developer连接ASP.Net登录控件 [英] Connecting ASP.Net Login Control With SQl Server 2008 R2 Developer
问题描述
我已使用登录控件创建了一个Web表单。现在我要验证UserName&密码与SQL数据库中的表上的数据(用户名和密码作为表的字段)。我是ASP.Net&的新手。我需要你的帮助谢谢。
我在win 7专业64位使用VS 2010 .net Framework 4.0! SQl server 2008 R2 Developer
你可以看一下如何实施表单身份验证 和管理具有角色的用户 。 Asp.NET中提供了用于用户身份验证的功能。
使用数据库的可能方法:
< pre lang =c#> SqlConnection conn = new SqlConnection();
conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings [ leave]。ConnectionString ;
conn.Open();
string uname = TextBox1.Text;
string ps = Textbox2.Text;
SqlCommand cmd = new SqlCommand( 选择uname,来自emp_details的pswd,其中uname = @ uname和pswd = @ ps,conn);
cmd.Parameters.Add( new SqlParameter( @uname, id here here));
cmd.Parameters.Add( new SqlParameter( @ps, 这里的密码));
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
/ * 对经过身份验证的用户执行操作* /
}
- 附加说明:
建议在数据库中以加密格式存储密码(不是纯文本)。您可以使用算法。例如,请查看简单字符串加密和解密
尝试
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings [SampleConnectionString]。ToString());
con.Open();
SqlCommand com = new SqlCommand(select username from registration where username ='+ Txtusername.Text +',con);
SqlDataReader dr = com.ExecuteReader();
if(!dr.Read())
{
labLogin.Text =(无效用户);
}
其他
{
if(dr [ 0] .ToString()== Txtpassword.Text)
{
Re sponser.Redirect(Select.aspx);
}
else
{
labLogin.Text = (密码错误);
Txtpassword.Focus();
}
}
}
catch(例外情况)
{
labLogin.Text =ex+ ex.Message;
}
对于你的问题评论
>好的我已经访问过那个然后我想验证登录控件的用户名和>来自表中的密码SQL数据库。那我该怎么做呢? >请帮帮我!
我想这是你的要求。
SqlConnection conn = new SqlConnection();
conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings [ connection]。ConnectionString ;
conn.Open();
string username = TextBox1.Text;
string password = Textbox2.Text;
SqlCommand cmd = new SqlCommand( 选择uname,来自emp_details的pswd,其中uname = @ uname和pswd = @ ps,conn);
cmd.Parameters.Add( new SqlParameter( @uname, username));
cmd.Parameters.Add( new SqlParameter( @ps, password));
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
if (dr.HasRows)
{
dr.close();
// 您的主页或Ur需要;
// 例如:// Response.Redirect(Home.aspx);
/ / 作为一个初学者,以上就足够了你必须在接下来的页面中显示Hi Chiranthaka Sampath等更多页面中的用户名你必须移动< b>会话。
会话[ username] = txtusername.Text;
会话[ password] = txtpassword.Text;
// 我的会话方式,只要你显示你可以从会话中得到的名字..
}
else
{
dr.close();
msg.Showalert( 用户名或密码无效);
}
con.close();
}
I have created a web form with the login control. Now I want to validate the UserName & the Password with the data on a table in a SQL database (username & password as the fields of the table). I am new to ASP.Net & I need your help thanks.
I am using VS 2010 .net Framework 4.0 in win 7 professional 64 bit! SQl server 2008 R2 Developer
You can have a look at How to implement forms authentication and Managing users with roles. There are functionalities available in Asp.NET for user authentication.
Possible method using database:
SqlConnection conn = new SqlConnection(); conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["leave"].ConnectionString; conn.Open(); string uname = TextBox1.Text; string ps = Textbox2.Text; SqlCommand cmd = new SqlCommand("Select uname, pswd from emp_details where uname =@uname and pswd =@ps", conn); cmd.Parameters.Add(new SqlParameter("@uname", "id here")); cmd.Parameters.Add(new SqlParameter("@ps", "password here")); SqlDataReader dr = cmd.ExecuteReader(); if (dr.Read()) { /*Perform operations for authenticated users*/ }
-Additional note:
Its advisable to store password in encrypted format in database(Not as plain text). You can make use of algorithm. For example, have a look at Simple String Encryption and Decryption
try
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["SampleConnectionString"].ToString());
con.Open();
SqlCommand com = new SqlCommand("select password from Registration where username='" + Txtusername.Text + "'", con);
SqlDataReader dr = com.ExecuteReader();
if (!dr.Read())
{
labLogin.Text = ("Invalid User");
}
else
{
if (dr[0].ToString() == Txtpassword.Text)
{
Response.Redirect("Select.aspx");
}
else
{
labLogin.Text = ("Wrong Password");
Txtpassword.Focus();
}
}
}
catch (Exception ex)
{
labLogin.Text = "ex" + ex.Message;
}
For Your Question comment
>Ok I have visited that and then I want to validate that login control's username and >the password from a table that in a SQL database. Then how am I going to do that? >Please help me!
I guess is this ur requirement.
SqlConnection conn = new SqlConnection(); conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["connection"].ConnectionString; conn.Open(); string username = TextBox1.Text; string password = Textbox2.Text; SqlCommand cmd = new SqlCommand("Select uname, pswd from emp_details where uname =@uname and pswd =@ps", conn); cmd.Parameters.Add(new SqlParameter("@uname", "username")); cmd.Parameters.Add(new SqlParameter("@ps", "password")); SqlDataReader dr = cmd.ExecuteReader(); dr.Read(); if (dr.HasRows) { dr.close(); //Your Homepage or Ur need; //Eg:// Response.Redirect("Home.aspx"); // As a beginner the above one is enough in future u have to show the user name in the further pages like Hi Chiranthaka Sampath in the next pages u have to move with Session. Session["username"] = txtusername.Text; Session["password"] = txtpassword.Text; //My means of Session wherever u show the name u can get from the Session.. } else { dr.close(); msg.Showalert("Invalid username or password"); } con.close(); }
这篇关于使用SQl Server 2008 R2 Developer连接ASP.Net登录控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!