c# - 自定义登录 ASP.NET C#

标签 c# asp.net asp.net-authentication

我目前正在 ASP.NET 中进行自定义登录。我修改了登录控件的代码以使用我的数据库而不是 Aspnet 表。这是我的代码示例;

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


public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    // Custom login control
    protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {
        try
        {
            string uname = Login1.UserName.Trim();
            string password = Login1.Password.Trim();

            bool flag = AuthenticateUser(uname, password);
            if (flag == true)
            {
                e.Authenticated = true;
                Login1.DestinationPageUrl = "Default.aspx";
            }
            else
                e.Authenticated = false;
        }
        catch (Exception)
        {
            e.Authenticated = false;
        }
    }

    private bool AuthenticateUser(string uname, string password)
    {
        bool bflag = false;
        string connString = "Server=DEVSERVER;User ID=sa;Password=whatpassword;Database=CommonUser";
string connstring2 = "Server=DEVSERVER;User ID=sa;Password=whatpassword;Database=Admins";
        string strSQL = "Select * from dbo.Users where Username ='" + uname + "' and Password ='" + password + "'";
        DataSet userDS = new DataSet();
        SqlConnection m_conn;
        SqlDataAdapter m_dataAdapter;
        SqlCommand m_Command;
        try
        {
            m_conn = new SqlConnection(connString);
            m_conn.Open();
            m_dataAdapter = new SqlDataAdapter(strSQL, m_conn);
            m_dataAdapter.Fill(userDS);
            m_conn.Close();
        }
        catch (Exception)
        {
            userDS = null;
        }

        if (userDS != null)
        {
            if (userDS.Tables[0].Rows.Count > 0)
                bflag = true;
        }
        return bflag;

    }
}

我有另一个数据库供管理员用户使用。所以我的问题是如何让它检查管理员用户的数据库。另外,如何限制普通用户访问某些页面,如 ~Admin/AdminPages.aspx?我目前正在尝试计算 This .

任何帮助将不胜感激;)

提前致谢

最佳答案

好吧,所以我要说这个,但要知道我的意思是用最好的方式...

你做错了!

尽管 Asp.Net already has this built in. 我并不反对使用自定义数据库当您可以使用非常 nice pluggable provider model 时,我什至不反对在方法中手动编码。 Asp.Net 内置的。我反对的是这段代码对 Sql Injection attack. 的开放程度。

考虑一下如果我输入 x' 会发生什么;删除表用户; -- 作为用户名? 坏事男人!!!!

好的,请认真按照我放在那里的链接,请至少 use parameterized queries!

关于c# - 自定义登录 ASP.NET C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5813340/

相关文章:

c# - 如何向主键和外键插入数据

c# - 使用 JQuery 加载 iframe(正确方法)

c# - 在类验证代码中创建还是创建验证类?

c# - 加密 ASP.NET 连接字符串的正确方法是什么?

asp.net-mvc - 重命名 ASP.NET MVC 项目时出错

asp.net - 在 ASP.NET 中;如何使用 ASP.NET 成员资格从远程 HTML 页面登录到另一个 ASPX 页面

c# - 银光,C# : Detect when there are no more images

C# 单选按钮 oncheckchanged 未触发

c# - gridview rowdatabound 事件中的 e.Row.DataItem 错误

asp.net - Azure Web 角色身份验证策略