使用内置表单身份验证在 ASP.NET MVC 中记录自定义操作
我正在使用内置的 表单身份验证 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道您是否可以使用
aspnetWebEventEvents
来实现这一点,听起来您想记录特定的情况,但我认为这不是这个功能的意图。您尝试过 ActionFilters 吗?您可以用它进行真正特定的日志记录,例如,您可以检查结果类型以查看当用户尝试使用该操作时发生的情况,并且您可以通过 ActionExecutedContext 访问 RouteData 值:
然后你只需用此属性装饰你想要记录的操作:
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
:And then you just Decorate the actions you want to log with this attribute: