How to manipulate WPF GUI based on user roles

前端 未结 2 859
抹茶落季
抹茶落季 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条回答
  •  生来不讨喜
    2020-12-13 17:08

    
    

    In your C# code:

    public Visibility AdministratorVisibility
    {
        get 
        { 
            MyPrincipal.CurrentPrincipal.IsInRole("Administrator") ? Visibility.Hidden : Visibility.Visible; 
        }
    }
    

    You can do the same thing for implementing something for IsReadOnly. If the user's role can change (I'm not sure how these user roles work), you can implement INotifyPropertyChanged and do NotifyPropertyChanged("AdministratorVisibility"), otherwise you could probably change the BindingMode to BindingMode.OneTime and skip implementing the notifications.

    This probably isn't a whole lot better than what you're doing already, but it's probably as good as you're going to get.

提交回复
热议问题