自定义授权属性的其他参数? [英] Custom Authorize Attribute additional Param?

查看:207
本文介绍了自定义授权属性的其他参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时寻找一种方式来定制自己的授权属性,这样我可以用我自己的MembershipProvider正确地执行它。

im looking for a way to customize my Authorize Attribute so that i can implement it correctly with my own MembershipProvider.

我需要的是有例如IsInRoles(字符串角色,诠释烫发),在其他的话,我想有一个拥有新IsinRoles更换或者建立另一种方法来归档这个结果。

What i need is to have the IsInRoles(string role, int perm) for example, in other word, i want to have it replace with a new IsinRoles or maybe create another method to archive this result.

这可能吗?或者我需要编写不同的授权属性?

Is it possible? or i need to write a different authorize attribute?

非常感谢您对我们的关注...

Thank you very much for your concern...

PS:IM工作的ASP.net MVC的,所以我需要有[Authorize]过滤器和运行

PS: im working on ASP.net MVC, so i need to have the [Authorize] filter up and running.

推荐答案

我觉得你可以只是一个公共属性添加到自定义AuthorizeAttribute。

I think you can just add a public property to your custom AuthorizeAttribute.

public class CustomAuthorizeAttribute : AuthorizeAttribute
{
    /// <summary>
    /// Add the allowed roles to this property.
    /// </summary>
    public YourCustomRoles RequiredRole;

    public int YourCustomValue;

    /// <summary>
    /// Checks to see if the user is authenticated and has the
    /// correct role to access a particular view.
    /// </summary>        
    /// <param name="httpContext"></param>
    /// <returns></returns>
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        if (httpContext == null) throw new ArgumentNullException("httpContext");

        // Make sure the user is authenticated.
        if (httpContext.User.Identity.IsAuthenticated == false) return false;

        // Can use your properties if needed and do your checks
        bool authorized = DoSomeCustomChecksHere();

        return authorized;
    }
}

使用我想会(还没有尝试过,虽然):

Usage I think would be (haven't tried it though):

[CustomAuthorizeAttribute (RequiredRole=MyCustomRole.Role1 | MyCustomRole.Role2, YourCustomValue=1234)]

这篇关于自定义授权属性的其他参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆