使用内置表单身份验证在 ASP.NET MVC 中记录自定义操作

发布于 2024-08-13 17:49:24 字数 671 浏览 2 评论 0原文

我正在使用内置的 表单身份验证 ASP.NET MVC。我使用 aspnet_regsql 向导将所有必要的表添加到 Sql Server,并且将其全部集成并完美运行。现在我可以添加用户、登录、执行控制授权等等。

下一步是在用户执行操作时添加一些基本日志。例如,我想记录谁使用用户管理从数据库中删除了用户,或者何时有人访问了包含受限信息的视图(这些只是示例)。

我已经看到,随着身份验证而来的是一个名为 aspnetWebEventEvents 的表,但似乎它仅用于存储网站上发生的异常,并且它是使用 web.config 配置的,并且所有操作都是自动执行的。我只想调用一个类来记录自定义操作,而无需手动填写所有字段:

Logger.Log("Action performed");

并自动存储用户、时间和所有这些字段。

我确信必须有一个已经实现的类来完成这些事情,并且我不想重写已经完成的东西。

我尝试使用 SqlWebEventProvider,这似乎是内部调用的类,但该类似乎是内部的,因此不打算使用它。

也许我错了,这并不是为了记录自定义操作,我应该更好地使用自定义操作创建自己的表......

I am using the built-in forms authentication that comes with asp.net mvc. I added all the necessary tables to Sql Server using aspnet_regsql wizard and I have it all integrated and working perfect. Now I can add users, log in, perform control Authorization and all these things.

The next step is to add some basic logs when a user performs an action. For example, I want to log who has deleted an user from the database using the UserAdministration, or when someone has visited a view with restricted information (these are just examples).

I've seen that with the authentication comes a table called aspnetWebEventEvents but seems that it is intended only to store exceptions occurred on the website and it is configured with web.config and it's all performed automatically. I would like just to call a class to log custom actions without having to fill manually all the fields:

Logger.Log("Action performed");

And store automatically the user, time and all these fields.

I'm sure there has to be an already implemented class to do this stuff and I don't want to rewrite something that it is already done.

I tried using SqlWebEventProvider, that seems to be the one that is called internally but this class seems to be internal so it is not intended to be used.

Maybe I'm wrong, this is not intended to log custom actions and I should better create my own table with custom actions...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

楠木可依 2024-08-20 17:49:24

我不知道您是否可以使用 aspnetWebEventEvents 来实现这一点,听起来您想记录特定的情况,但我认为这不是这个功能的意图。

您尝试过 ActionFilters 吗?您可以用它进行真正特定的日志记录,例如,您可以检查结果类型以查看当用户尝试使用该操作时发生的情况,并且您可以通过 ActionExecutedContext 访问 RouteData 值:

public class LogActionFilter : ActionFilterAttribute 
    {
        public override void  OnActionExecuted(ActionExecutedContext filterContext)
        {

            if (filterContext.Exception != null)
            {
                //Log - Exception Cases
            }
            else if(filterContext.Result.GetType() == typeof(RedirectToRouteResult))
            {
                //Log - Redirect to other Action
            }
           else 
            {
                //Log the normal behavior 
            }
        }

   }

然后你只需用此属性装饰你想要记录的操作:

[LogActionFilter]
public ActionResult Edit(int id)
{
}

I dont know if you can accomplish that with aspnetWebEventEvents, it sound like you want to log specific situations which i don't think is the intention of this buld in feature.

Have you tried ActionFilters?, you can do really specific logging with this, for example you can check the Result Type to see what happened when the user try to use that action, and you have access to the RouteData Values through the ActionExecutedContext:

public class LogActionFilter : ActionFilterAttribute 
    {
        public override void  OnActionExecuted(ActionExecutedContext filterContext)
        {

            if (filterContext.Exception != null)
            {
                //Log - Exception Cases
            }
            else if(filterContext.Result.GetType() == typeof(RedirectToRouteResult))
            {
                //Log - Redirect to other Action
            }
           else 
            {
                //Log the normal behavior 
            }
        }

   }

And then you just Decorate the actions you want to log with this attribute:

[LogActionFilter]
public ActionResult Edit(int id)
{
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文