65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 ASP.NET 2.0 创建审计功能

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.86/5 (5投票s)

2007年1月8日

2分钟阅读

viewsIcon

91013

downloadIcon

1109

如何使用 ASP.NET 审计用户数据变更

Sample image

引言

本文更新描述了如何创建一个审计工具,该工具使用 ASP.NET 网页跟踪和记录任何字段的变更及其旧值和新值。要查看项目实际效果,您可以下载代码。但是,您需要更改数据库连接属性。

背景

当我遇到这个问题时,我找到了各种解决方案,从 SQL Server 触发器到需要更多 Web 控件知识的自定义解决方案。但是,我的解决方案具有以下优点:

  • 仅记录并跟踪已更改字段或字段的变更。例如,一个解决方案使用触发器来填充审计变更,但它包含了 UPDATE SQL 语句中的所有字段。这意味着它会将未更改的字段填充到 Audit 表中,从而导致我的审计表呈指数级增长。
  • 利用已经包含在 .NET Framework 中的事件。在本例中,我使用关键事件对象来一致地记录和比较数据变更,无论使用 GridView 还是 DetailsView
  • 使用现有属性来使审计跟踪起作用。在 .NET Framework 中已经很好地构建了 GridViewDetialsView 控件中的事件属性。这允许我们通过返回的集合对象查看新值和旧值。

Using the Code

对于 GridViewDetailsView,模式实现是相同的。只需将适当的事件对象作为参数传递即可。以下是 GridViewDetailsView 的重载方法的示例:

// GridView Method overload

public static void updateFieldAudits(GridViewUpdatedEventArgs e)
{
    foreach (DictionaryEntry newValues in e.NewValues)
    {
        int i = 0;

        string newKeyCol = newValues.Key.ToString();
        foreach (DictionaryEntry oldVals in e.OldValues)
        {
            string oldKeyCol = oldVals.Key.ToString();

            if (oldKeyCol == newKeyCol)
            {
                break;
            }
            i++;
        }
        string oldVal = (string)e.OldValues[i];

        if (newValues.Value != null)
        {
            string newVal = newValues.Value.ToString();

            if (oldVal != newVal)
            {
                MyDBDAL.insertAuditChanges(newKeyCol, oldVal, newVal);
            }
        }
        else
        {
            if (!String.IsNullOrEmpty(oldVal))
            {
                MyDBDAL.insertAuditChanges(newKeyCol, oldVal, "");
            }
        }
    }
}
// DetailsView Method overload

public static void updateFieldAudits(DetailsViewUpdatedEventArgs e)
{
    foreach (DictionaryEntry newValues in e.NewValues)
    {
        int i = 0;

        string newKeyCol = newValues.Key.ToString();
        foreach (DictionaryEntry oldVals in e.OldValues)
        {
            string oldKeyCol = oldVals.Key.ToString();
            if (oldKeyCol == newKeyCol)
            {
                break;
            }
                i++;
            }
            string oldVal = (string)e.OldValues[i];

            if (newValues.Value != null)
            {
                string newVal = newValues.Value.ToString();

                if (oldVal != newVal)
                {
                MyDBDAL.insertAuditChanges(newKeyCol, oldVal, newVal);
            }
        }
        else
        {
            if (!String.IsNullOrEmpty(oldVal))
            {
                MyDBDAL.insertAuditChanges(newKeyCol, oldVal, "");
            }
        }
    }
}

为了记录变更,GridView 感兴趣的事件是 RowUpdated 方法。下面的示例捕获此事件,并允许您将 GridViewUpdatedEventArgs 事件对象参数传递给 updateFieldAudits 方法。

protected void GridView1_RowUpdated(object sender, 
    GridViewUpdatedEventArgs e)
{
    AuditFields.updateFieldAudits(e);
}

对于 DetailsView 代码隐藏文件,我们捕获 ItemUpdated 事件方法

protected void DetailsView1_ItemUpdated(object sender, 
    DetailsViewUpdatedEventArgs e)
{ 
    AuditFields.updateFieldAudits(e);
}

最后,我们需要将 Web 控件事件与 GridViewDetailsView 关联,如下所示:

OnRowUpdated="GridView1_RowUpdated" 
OnItemUpdating="DetailsView1_ItemUpdating"

完成了!它可移植于 Web 控件,您可以添加自己的数据库更新函数来记录审计数据变更,或者创建自定义业务对象。

历史

  • 2007 年 1 月 8 日 -- 发布原始版本
  • 2007 年 6 月 8 日 -- 更新文章
© . All rights reserved.