Quantcast
Viewing all articles
Browse latest Browse all 18858

Setting WPF UI Permissions in code-behind

I am looking to see if I can do the following in WPF:

A user opens the WPF application and using the users AD name I will check the users role(s) in a custom DB. This will be something similar to asp.net membership db.

Depending on the user role the WPF will show certain controls.

So for example, if I was an Admin then I would have access to everything but if I was read-only user then I would have limited access to the application.

I know I can set the visibility using the following example:

public class RoleToVisibilityConverter : MarkupExtension,IValueConverter
{
    public RoleToVisibilityConverter()
    {

    }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var principal = value as GenericPrincipal;
        bool IsValidUser = false;
        if (principal != null)
        {
            foreach (String role in parameter.ToString().Split(';'))
            {
                if (principal.IsInRole(role))
                {
                    IsValidUser = true;
                    break;
                }  
            }
            return IsValidUser ? Visibility.Visible : Visibility.Collapsed;
        }

        return null;
    }

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

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }
}

And in the XAML I add the following binding for visibility

Visibility="{Binding Source={x:Static systhread:Thread.CurrentPrincipal}, ConverterParameter=admin;editor;readonly, Converter={rv:RoleToVisibilityConverter}}

But what I want to do is to not have to add this in xaml.

I would like to be able to get the users role and all of it's permissions and depending on the window load the UI permissions.

Reason is that I would like to be able to change what a read-only role can see/do without having to change the xaml.

Does anyone know if this is possible and if so is it best practice.

Thanks in advance.

Noel





Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>