授权属性

时间:2011-09-14 05:28:08

标签: c# wpf c#-4.0 custom-attributes

我有一个来自客户端的WPF应用程序,我需要为类中的某些方法添加一个IsInRoleAtrribute。

所以我创建了像这样的IsInRole类作为测试;

namespace CustomAttributeSample
{
    [System.AttributeUsage(System.AttributeTargets.Method)]
    public class IsInRoleAttribute : Attribute
    {
        public string Role { get; set; }

        public IsInRoleAttribute()
        {
            //open the xml file
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load("Data.xml");
        }

        public override bool Match(object obj)
        {
            return false;
        }

        public override bool Equals (object obj)
        {
            return false;
        }
    }
}

我装饰这样的方法,到目前为止再次作为测试;

    [IsInRole(Role="Admin")]
    private bool Save()
    {

但是我无法阻止该方法被执行。

我可以在MVC中做到这一点没有probs,但这是我多年来的第一个WPF应用程序,所以我知道我在这里遗漏了一些东西。

1 个答案:

答案 0 :(得分:1)

不是很熟悉,但试着改为:

[AuthorizationAttribute(AuthorizationType.Allow, "Admin")]

如果你使用MVVM范例,这应该可行; - )

来源:WPF Command and claim/role based security