自定义授权属性
我正在实现 CustomAuthorizeAttribute。我需要获取正在执行的操作的名称。如何获取在我要重写的 AuthorizeCore 函数中执行的当前操作名称的名称?
I am implementing a CustomAuthorizeAttribute. I need to get the name of the action being executed. How can i get the name of current action name getting executed in the AuthorizeCore function which i am overriding ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您正在使用缓存(或计划使用),则覆盖
AuthorizeCore
,就像 Darin Dimitrov 在 这个答案是一个更安全的选择:其原因记录在 MVC 源代码本身:
AuthorizeAttribute.cs(行72-101)
即使您不打算使用缓存,这两个神奇的字符串似乎也只是为了让您安心而付出的小代价(以及您自己避免的潜在麻烦。)如果您仍然想覆盖
OnAuthorization
相反,您至少应该确保请求没有被缓存。有关更多背景信息,请参阅 Levi 的这篇文章。If you're using cache (or have plans to), then overriding
AuthorizeCore
, like Darin Dimitrov shows in this answer is a much safer bet:The reason for this is documented in the MVC source code itself:
AuthorizeAttribute.cs (lines 72-101)
Even if you didn't plan on using cache, those two magic strings seem a small price to pay for the peace of mind you get in return (and the potential headaches you save yourself.) If you still want to override
OnAuthorization
instead, you should at least make sure the request isn't cached. See this post by Levi for more context.您可以像这样获取操作名称:
编辑:
如果您想从 AuthorizationAttribute 继承,则需要重写 OnAuthorization 方法。
You can get the Action Name like this:
EDIT:
If you want to inherit from the AuthorizationAttribute you'll need to override the OnAuthorization method.