Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我如何使用;角色“;自定义“授权”属性时的参数_C#_Asp.net Mvc_Asp.net Mvc 3_Authorization - Fatal编程技术网

C# 我如何使用;角色“;自定义“授权”属性时的参数

C# 我如何使用;角色“;自定义“授权”属性时的参数,c#,asp.net-mvc,asp.net-mvc-3,authorization,C#,Asp.net Mvc,Asp.net Mvc 3,Authorization,我正在使用MVC3、C#和Razor 我正在尝试自定义Authorize属性 代码片段: public class AuthorizeCustomAttribute : AuthorizeAttribute { protected override bool AuthorizeCore(HttpContextBase httpContext) { var authorized = base.AuthorizeCore(httpContext); i

我正在使用MVC3、C#和Razor

我正在尝试自定义Authorize属性

代码片段:

public class AuthorizeCustomAttribute : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        var authorized = base.AuthorizeCore(httpContext);
        if (!authorized)
        {
            // The user is not authenticated
            return false;
        }

        var user = httpContext.User;
        if (user.IsInRole("Admin")) // This should not be hardcoded, but use the Roles parm somehow. This is the core of my question here.
        {
            return true;
        }
使用该属性时,其外观如下所示:

[AuthorizeCustom(Roles="Admin,User")]
在自定义属性类中访问这个“Roles”参数及其值将非常有用,但我看不出如何做到这一点。“httpContext”变量中必须有一个属性,但我无法找到它

想法?

以下语法:

[Authorize(Roles = "Admin,User")]
使用。它实际上是属性类中的一个属性。因为您的类派生自
AuthorizeAttribute
,它包含:


您应该能够将其用作
AuthorizeCustom

的构造函数中的参数,我是否必须专门测试此Roles属性,或者这是在基类中发生的,因此是否存在可以像RolesValid一样调用的结果属性?我看不到任何东西,所以我猜测我需要执行“IfIsInRoles(Roles){return true}”,我认为它自动合并到“base.AuthorizeCore(httpContext)”中?是的,刚刚确认:)是的,授权方法将考虑角色。
public string Roles { get; set; }