Click here to Skip to main content
15,886,799 members
Articles / Desktop Programming / WPF

Implement UI Element Authorization in WPF

Rate me:
Please Sign up or sign in to vote.
5.00/5 (18 votes)
4 Nov 2011CPOL5 min read 60.2K   2.6K   41  
How to implement UI element authorization/access control in WPF using the ICommand interface and Markup Extension
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using UIAuthorization.Providers;


namespace UIAuthorization
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            Login();
            InitializeComponent();
            DataContext = new MainWindowVM();
        }

        private void Login()
        {
            //  Try out each section to see how it applies to your view screen
            string[] myACL;
            
            //  You can click the 'Read' button and close the window
            myACL = new string[] {
                ACL.CAN_READ,
                ACL.CAN_CLOSE
            };

            //  You can see both images but cannot click any button or close the window
            /*
            myACL = new string[] {
                ACL.CAN_VIEW
            };
            */

            //  You can close the window but cannot click any button
            /*
            myACL = new string[] {
                ACL.CAN_CLOSE
            };
            */

            //  You can see both images and click any button but cannot close the window
            /*
            myACL = new string[] {
                ACL.CAN_VIEW,
                ACL.CAN_CREATE,
                ACL.CAN_READ,
                ACL.CAN_UPDATE,
                ACL.CAN_DELETE,
            };
            */

            //  Full trust
            /*
            myACL = new string[] {
                ACL.CAN_VIEW,
                ACL.CAN_CREATE,
                ACL.CAN_READ,
                ACL.CAN_UPDATE,
                ACL.CAN_DELETE,
                ACL.CAN_CLOSE
            };
            */

            AuthProvider.Initialize<DefaultAuthProvider>(myACL);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Singapore Singapore
Elvin Cheng is currently living in Woodlands, Singapore. He has been developing applications with the .NET Framework, using C# and ASP.NET since October 2002. Elvin specializes in building Real-time monitoring and tracking information system for Semi-conductor manufacturing industry. During his spare time, he enjoys reading books, watching movie and gym.

Comments and Discussions