如何根据用户角色操纵WPF图形用户界面 [英] How to manipulate WPF GUI based on user roles

查看:143
本文介绍了如何根据用户角色操纵WPF图形用户界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用.NET的的IIdentity和IPrincipal的对象,基于角色的安全性,我在基于角色的当前用户具​​有修改显示控制器的步骤。

I am using .NET's IIdentity and IPrincipal objects for role based security, and I am at the step of modifying controls shown based on roles the current user has.

我的问题是推荐的方法是在一个WPF窗口启用/禁用领域 - 显示/隐藏依赖IIdentity.IsInRole呼叫类型的字段

My question is what the recommended method is for enabling/disabling fields in a WPF window - showing/hiding fields dependent on IIdentity.IsInRole type calls.

可以这样做中XAML,或做我必须抽象到这一代码是什么,我认为是在后面的代码有点乱;

Can this be done in XAML, or do I have to abstract this into code with what I think is a bit messy in the code behind;

this.txtUserName.IsReadOnly = !MyPrincipal.CurrentPrincipal.IsInRole("Administrator");
this.mnuCreateUser.Visibility = MyPrincipal.CurrentPrincipal.IsInRole("Administrator");
 ? Visibility.Hidden : Visibility.Visible;



(注意:依赖于角色,因此用户不会看到/看只读元素,他们没有获得)

(Note; my code checks roles when executing functions, what I am looking to do is modifying the GUI dependent on the roles, so users do not see/see readonly elements that they do not have access to)

推荐答案

虽然前面的回答将工作,对我来说,它看起来有点丑检测逻辑对象的知名度。我会用转换为...

Although previous answer will work, to me it looks little ugly to detect visibility in logic objects. I would use converter for that...

<Control Visibility={Binding Path=CurrentPrincipal, Converter={StaticResource RoleToVisibilityConverter}, ConverterParameter=Administrator}/>

和则转换器本身

public class RoleToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var principal = value as Principal;
        if(principal != null) {
            return principal.IsInRole((string)parameter) ? Visibility.Visible : Visibility.Collapsed;
        }

        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
         throw new NotImplementedException();
    }
}

这篇关于如何根据用户角色操纵WPF图形用户界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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