How to manipulate WPF GUI based on user roles

前端 未结 2 851
抹茶落季
抹茶落季 2020-12-13 16:15

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.

My

2条回答
  •  -上瘾入骨i
    2020-12-13 16:45

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

    
    

    And then the converter itself

    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();
        }
    }
    

提交回复
热议问题