如何选择登录用户并检查其是否为acccess类型 [英] How to to select the user that login and check them for type of acccess

查看:110
本文介绍了如何选择登录用户并检查其是否为acccess类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。我有3层架构asp.net

i想要选择我的数据并检查它们是否登录。

但是





我的DAL代码:



hi all. i have 3 tier architecture asp.net
i want to select my data and check them for login.
but


my DAL code:

public DataTable findrecords(string UserName)

       {
               connect.Open();
               SqlCommand cmd = new SqlCommand("Select * from UserInfo where UserName= '" + UserName + "'", connect);
               DataTable dt = new DataTable();
               SqlDataAdapter sda = new SqlDataAdapter(cmd);
               sda.Fill(dt);
               return dt;
       }





和BLL代码:



and BLL code:

public void selectRecords()
       {
           da.findrecords(UserName);

       }





或:



or :

cmd.Parameters.AddWithValue("@UserName", UserName);
            da.findrecords(UserName);





和我的代码隐藏:



and my codebehind:

bll.UserName = UserNameTextBox.Text;
bll.selectRecords();
if (bll.UserTyp == 0)
{
    Session.Add("Msg_", "user you are un active");
    Response.Redirect("~/ShowMessage.aspx");
}
else
    if (bll.UserTyp == 1)
    {
        Response.Redirect("~/index.aspx");
    }





我尝试过:



大家好。我有3层架构asp.net

我想选择登录的用户并检查它们的类型。

每件事都没问题。并且没有显示错误。但是每个具有任何访问类型的用户都是零,unactive是我的代码是正确的,数据不返回?



What I have tried:

hi all. i have 3 tier architecture asp.net
i want to select the user that login and check them for type of acccess.
every thing is ok. and no error is shown. but every user with any type of access is zero and "unactive" is my code is correct and data not return or not?

推荐答案

SqlCommand cmd = new SqlCommand("Select * from UserInfo where UserName= '" + UserName + "'", connect);



不是你问题的解决方案,而是你遇到的另一个问题。

永远不要通过连接字符串来构建SQL查询。迟早,您将使用用户输入来执行此操作,这会打开一个名为SQL注入的漏洞,这对您的数据库很容易并且容易出错。

名称中的单引号你的程序崩溃。如果用户输入像Brian O'Conner这样的名称可能会使您的应用程序崩溃,那么这是一个SQL注入漏洞,崩溃是最少的问题,恶意用户输入,并且它被提升为具有所有凭据的SQL命令。

SQL注入 - 维基百科 [ ^ ]

SQL注入 [ ^ ]

按示例进行SQL注入攻击 [ ^ ]

PHP:SQL注入 - 手册 [ ^ ]

SQL注入预防备忘单 - OWASP [ ^ ]


Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]


好的,您正在调用名为tbl_User_SelectRow的数据库中的存储过程,该存储过程需要传入一个参数,称为@UserName。您的代码不提供该参数。



快速Google C#sql参数化查询 [ ^ ]有大量有关如何操作的信息。
OK, you're calling a stored procedure in the database called "tbl_User_SelectRow" which expects a parameter to be passed in, called "@UserName". You're code doesn't supply that parameter.

A quick Google for "C# sql parameterized query[^]" has tons of information on how to do it.


业务逻辑通常验证在发布到数据库之前。示例:

Business Logic usually validates before posting to the DB. Example:
if (!string.IsNullOrEmpty(UserNameTextBox.Text)
{
    bll.UserName = UserNameTextBox.Text;
    bll.bl_bind();
    if (bll.UserTyp == 0)
    {
        Response.Redirect("~/ShowMessage.aspx");
    }
else
{
   // handle invalid (empty) username...
}


这篇关于如何选择登录用户并检查其是否为acccess类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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