ASP.NET中的登录代码 [英] Login code in ASP.NET

查看:100
本文介绍了ASP.NET中的登录代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是登录的代码,但它不起作用,请你帮我...



this is the code of login but it is not working can u please help me out...

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class sigin_in : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
   
    }
    SqlConnection con = new SqlConnection("Data Source=KRISHN-PC;Initial Catalog=sign;Persist Security Info=True;User ID=sa;Password=***********");
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlDataAdapter obj = new SqlDataAdapter("select from login where userid = '"+TextBox1.Text+"'and password = '"+TextBox2.Text +"'",con);
        DataSet a = new DataSet();
        obj.Fill(a);
        //obj.Update(a);
        if (a.Tables[0].Rows.Count > 0)
        {
            Response.Redirect("home.aspx");
        }
        else
        {
            Label.Text = " wrong pswd";
        }
    }

}

推荐答案

一个SqlDataAdapter需要一个Open() SqlConnection和数据在数据集之后需要关闭SqlConnection()



a SqlDataAdapter needs an Open() SqlConnection and after the data is in the dataset the SqlConnection needs to be Closed()

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
  
 public partial class sigin_in : System.Web.UI.Page
 {
	string connectToDB = "Data Source=KRISHN-PC;Initial Catalog=sign;Persist Security Info=True;User ID=sa;Password=***********";
	protected void Page_Load(object sender, EventArgs e)
	{

	}
 
	protected void Button1_Click(object sender, EventArgs e)
	{
		SqlConnection con = new SqlConnection(connectToDB);
		string myQuery = string.Format("select from login where userid = '{0}' and password = '{1}'", TextBox1.Text, TextBox2.Text);
		SqlDataAdapter obj = new SqlDataAdapter(myQuery, con);
		try
		{
			con.Open()
			DataSet a = new DataSet();
			obj.Fill(a);
			
			if (a.Tables[0].Rows.Count > 0)
			{
				Response.Redirect("home.aspx");
			}
			else
			{
				Label.Text = " wrong pswd";
			}
		}
		catch (Exception err)
		{
		}
		finally
		{
			if (null != con && con.Connection.State == ConnectionState.Open)
				con.Close();
		}
	 }
 } 


虽然这不是解决方案,但我不想将其添加为一个简单的评论,因为我认为去那里很重要。



不要使用 sa 帐户连接到任何数据库 - 此帐户至少需要重命名为其他内容。



如果您无法使用集成安全性,请为您的数据库创建特定帐户并确保授予尽可能少的私有化。



请谷歌了解有关保护数据库的更多信息。
Although this isn't a solution, I didn't want to add it as a plain comment, as I feel it is to important to go there.

Don't connect to any database using the sa account - also this account needs to be renamed to something else at least.

If you can't use integrated security then create a specific accounts for your database and make sure you grant the least amount of privaleges to each as possibly.

Have a google to learn more about securing your databases.


致电 SqlConnection 打开方法,然后将其传递给 SqlDataAdapter 的构造函数。 SqlDataAdapter 需要打开连接。并在select语句中指定列名。
Call the SqlConnection's Open method before passing it on to the SqlDataAdapter's constructor. SqlDataAdapter requires an open connection. And also specify the Column names in your select statement.


这篇关于ASP.NET中的登录代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆