自定义登录ASP.NET C#

Lou*_*upi 9 c# asp.net asp.net-authentication

我目前正在ASP.NET中进行自定义登录.我已经修改了Login Control的代码以使用我的数据库而不是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;

    }
}
Run Code Online (Sandbox Code Playgroud)

我有另一个管理员用户数据库.所以我的问题是如何让它检查管理员用户的数据库.另外,我如何限制某些页面的常见用户,例如~Admin/AdminPages.aspx?我现在正试图想出这个.

任何帮助将非常感激 ;)

提前致谢

Jos*_*osh 21

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

你做错了!

虽然Asp.Net 已经内置了这个,但我并不反对使用自定义数据库.我甚至不打算在一个方法中反对手动编码,因为你可以使用非常好的可插拔提供程序模型 Asp.Net已经内置.我反对的是这个代码对Sql Injection攻击有多宽.

考虑一下如果我输入x'; DROP TABLE Users; --用户名会发生什么?坏男人!!!!

好的,请认真关注我放在那里的链接,请至少使用参数化查询!


Jam*_*ris 6

您发布的代码存在很多问题.

string strSQL = "Select * from dbo.Users where Username ='" + uname + "' and Password ='" + password + "'";

从来没有永远做字符串连接来构建查询.这使您的应用程序对SQL注入开放.请改用参数化查询.

为什么管理员和普通用户有单独的数据库?您不会将所有登录存储在单个数据库中的单个表中.然后使用单个字段"IsAdmin"或使用单独的Roles表和UsersInRoles表来确定哪些用户是Admin.

您不使用内置会员提供商的原因是什么?您将内置提供程序配置为使用任何数据库,而不仅仅是AppData\aspnet.mdf.

您通常使用角色将不同的页面限制为不同的用户,这可以在授权元素内的web.config文件中设置.

如果您真的想要创建自定义简单身份验证系统,请使用http://csharpdotnetfreak.blogspot.com/2009/02/formsauthentication-ticket-roles-aspnet.html等 手动将用户角色手动分配给用户身份.