我目前正在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?我目前正在尝试弄清楚 这个.

任何帮助将非常感激 ;)

提前致谢

有帮助吗?

解决方案

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

你这样做是不对的!

我没有反对使用自定义数据库,尽管ASP.NET 已经内置了。 当您可以使用该方法时 不错的可容纳提供商模型 ASP.NET已内置。我反对的是,此代码对 SQL注射攻击。

考虑一秒钟,如果我打字会发生什么 x'; DROP TABLE Users; -- 作为用户名? 坏事人!

好的,非常认真地按照我在那里提出的链接,请至少请 使用参数化查询!

其他提示

您发布的代码有很多错误。

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

从来没有做过字符串串联来构建查询。 这使您的应用程序开放给SQL注入。改用参数化查询。

为什么您为管理员和普通用户有一个单独的数据库?您不会将所有登录名存储在单个数据库中的单个表中。然后,要么使用单个字段“ Isadmin”,要么使用单独的角色表和用户表来确定哪些用户是管理员。

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

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

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

我真的认为您根本不应该进行硬码数据库连接参数。是一种不好的做法,因为如果DB发生了变化,则必须重新编译。因此,您必须做的是实施自定义会员和角色提供商。看 本文。 基本上,您需要创建一个自定义类,该类从System.Web.Security.Roleprovider和System.Web.Security.MembershippRovider继承。会员资格管理用户和角色,好..用户权限。

设置所有设置后,您可以通过page.spx页面代码 - 障碍文件上的用户属性检查用户权限。

为了将我的声音添加到混音中。那些不从历史上学习的人注定要重复它。 ASP.NET成员资格系统是多年试图实施自己的用户身份验证系统的人。微软从中学到了。你应该。

使用会员提供者模型。如果您不想使用默认提供商,请实现自己的自定义提供商。但老实说,使用内置的提供商并将其适应您想要的任何东西非常容易。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top