C# asp.net mvc应用程序的专用授权属性

C# asp.net mvc应用程序的专用授权属性,c#,asp.net,asp.net-mvc,attributes,C#,Asp.net,Asp.net Mvc,Attributes,我们正在尝试为Asp.Net MVC创建一个更具体的[Authorize]属性 [AttributeUsage(AttributeTargets.All)] public class AuthoriseUser : System.Attribute { public AuthoriseUser(string PermRequest) { //Do Some Auth. } } 我们这样称呼它 [

我们正在尝试为Asp.Net MVC创建一个更具体的[Authorize]属性

 [AttributeUsage(AttributeTargets.All)] 
    public class AuthoriseUser : System.Attribute
    {
        public AuthoriseUser(string PermRequest)
        {
            //Do Some Auth.
        }
    }
我们这样称呼它

[AuthoriseUser("CanViewRoles")]
但在调试时,永远不会调用该函数


现在我们显然做了一些非常错误的事情,我看过PostSharp,但由于项目的性质,我不能使用它。仅使用.Net如何实现这一点?

属性用于“修饰”方法/属性/类,以便您可以赋予该方法额外的意义

通过使用某个属性装饰一个方法,这并不意味着每当调用该方法时都会执行该“属性”。
如果您想要这种行为,您确实需要查看代码编织器,例如PostSharp,以将额外的代码编织到您用属性修饰的方法/属性。

您是否不需要从
AuthorizeAttribute
派生类?请参见.NET中的属性用于修饰类或方法,但您需要编写代码来请求此信息。例如,要验证某个类是否用自定义属性修饰,可以使用以下内容:

var attributes = (AuthoriseUser[])typeof(YourType)
    .GetCustomAttributes(typeof(AuthoriseUser), true);

然后,您可以读取与此属性关联的元数据。

属性不用于扮演功能角色。您需要在项目中的某个地方使用反射编写代码,以读取类[以及道具、方法等]的类型元数据并查找应用于它的属性。根据应用的属性,您可以在运行时决定如何处理这些属性。通常,这是在库的基类中完成的

例如,在我们的项目中,我们有一个名为“Searchable”的属性。此属性应用于需要包含在搜索中的业务对象的属性。当客户端调用Search方法时,我们过滤掉所有用Searchable属性修饰的道具,然后构造查询对数据库进行搜索。实际上,我们在SearchableAttribute类中并没有任何与搜索功能相关的代码——事实上,在SearchableAttribute类中根本并没有任何代码

示例代码:

可搜索属性

/// <summary>
/// Holds mapping information of searchable fields of business objects.
/// </summary>
[global::System.AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
public sealed class SearchableAttribute : Attribute
{
    /// <summary>
    /// Initializes a new instance of the SearchableAttributeAttribute class.
    /// </summary>
    public SearchableAttribute()
    {
    }
}
//
///保存业务对象的可搜索字段的映射信息。
/// 
[全局::System.AttributeUsage(AttributeTargets.Property,Inherited=true,AllowMultiple=false)]
公共密封类SearchableAttribute:属性
{
/// 
///初始化SearchableAttributeAttribute类的新实例。
/// 
公共SearchableAttribute()
{
}
}
业务对象基类中的一种方法

    /// <summary>
    /// Provides collection of all Searchable Fields.
    /// </summary>
    /// <returns>DataField collection</returns>
    public IQueryable<DataField> GetSearchableDataFields()
    {
        PropertyInfo[] properties =
            this.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

        var entityFields = from PropertyInfo property in properties
                           where property.GetCustomAttributes(typeof(SearchableAttribute), true).Length > 0
                           select
                               new DataField(
                                   property,
                                   (SearchableAttribute)property.GetCustomAttributes(typeof(SearchableAttribute), true)[0]);

        return entityFields.AsQueryable();
    }
//
///提供所有可搜索字段的集合。
/// 
///数据字段收集
公共IQueryable GetSearchableDataFields()
{
PropertyInfo[]属性=
this.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
var entityFields=来自属性中的PropertyInfo属性
where property.GetCustomAttributes(typeof(SearchableAttribute),true)。长度>0
选择
新数据字段(
财产,,
(SearchableAttribute)属性。GetCustomAttributes(typeof(SearchableAttribute),true)[0]);
返回entityFields.AsQueryable();
}

你有这样的例子吗?在ASP.NET MVC之外,你的答案是正确的;然而,ASP.NET MVC操作过滤器是一种特殊的属性。它们作为MVC管道的一部分执行,并允许您处理某些事件。在ASP.NET MVC的上下文中,您的答案是错误的。