自定义属性方法级别授权

发布于 2024-11-04 02:13:19 字数 1355 浏览 4 评论 0原文

我正在尝试使用自定义属性来实现执行方法的授权权限。这是我现在所拥有的(我刚刚开始使用自定义属性):

[RequiredUserPermissions(UserPermissions.CanLoginViaSite)]
internal static bool HasDesiredPermissions()
{   
    //Execute body here if the attribute decorated permissions exist
    //for current user tracked as this._user (with permissions as
    //this._user.UserPermissions (of type UserPermissions (an enum))
}

//Custom attribute class
[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
internal sealed class RequiredUserPermissionsAttribute : Attribute
{
    private readonly UserPermissions _requiredPermission;

    public RequiredUserPermissionsAttribute(UserPermissions requiredPermission)
    { this._requiredPermission = requiredPermission; }

    public UserPermissions RequiredPermissions
    { get { return _requiredPermission; } }
}

我在 stackoverflow 上找到的最接近的问题是 使用属性提前从方法返回。如果我使用 asp.net mvc 框架,答案将是完美的,但不幸的是我现在处于 WCF 服务中,并且除了我现在拥有的简单数据库登录(通过 WCF 方法调用)之外实现整个授权层不是从时间线的角度来看,现在是可行的。

我相信 PostSharp 可能是最好的选择,但现在又不是一个可行的选择。我被困在这里了吗?我是否应该返回通过采用枚举并返回 bool 的方法来执行此操作。如果它能让我通过自定义属性实现此功能,我很高兴学习详细/复杂的内容。

任何有关我如何解决此问题的帮助将不胜感激。

I'm trying to use custom attributes to implement authorization permissions to execute a method. Here is what I have right now (i'm just starting on custom attributes):

[RequiredUserPermissions(UserPermissions.CanLoginViaSite)]
internal static bool HasDesiredPermissions()
{   
    //Execute body here if the attribute decorated permissions exist
    //for current user tracked as this._user (with permissions as
    //this._user.UserPermissions (of type UserPermissions (an enum))
}

//Custom attribute class
[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
internal sealed class RequiredUserPermissionsAttribute : Attribute
{
    private readonly UserPermissions _requiredPermission;

    public RequiredUserPermissionsAttribute(UserPermissions requiredPermission)
    { this._requiredPermission = requiredPermission; }

    public UserPermissions RequiredPermissions
    { get { return _requiredPermission; } }
}

The closest question that I've found on stackoverflow is Using an attribute to prematurely return from a method. The answer would have been perfect if I was using asp.net mvc framework but unfortunately I'm in a WCF Service right now and implementing whole Authorization layer beyond the simple database login that I have right now (through a WCF Method call) is not feasible right now from timeline perspective.

I believe PostSharp is probably the best option but again not a feasible option right now. Am I stuck here? Should I just go back to doing this through Methods that take enum and return bool. I am happy to learn detailed / complex stuff if it will let me implement this functionality through custom attiributes.

Any help with how I can go about this will be much appreciated.

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

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

发布评论

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

评论(1

绳情 2024-11-11 02:13:19

我不知道它是否适用于您的情况,但如果您正在编写 WCF 服务,为什么不直接从框架使用角色基础授权呢?它基于角色或基于声明,对于最简单的情况,您可以通过属性来定义它。
可以通过 自定义主体 或简单地使用框架中定义的标准角色提供者。

[PrincipalPermission(SecurityAction.Demand, Role = 'LoginViaSiteVisitors')]
internal static bool HasDesiredPermissions()
{
   //....
}

I don't know if it could apply to your case, but if you are writing a WCF service, why don't you use role base authorization directly from the framework? It's either role or claim based, and for the simplest cases you can define it by means of attributes.
The roles could be defined either via a Custom Principal or simply by using the standard role providers defined in the framework.

[PrincipalPermission(SecurityAction.Demand, Role = 'LoginViaSiteVisitors')]
internal static bool HasDesiredPermissions()
{
   //....
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文