wpf - Update Visibility of MenuItem when Thread.CurrentPrincipal changes -


i'am following this article add authentication wpf application. in mainview i'am binding visibility of menuitem currentprincipal. when role of currentprincipal admin want make menuitem visible. add line:

<menuitem visibility="{binding source={x:static threading:thread.currentprincipal}, converter={staticresource roletovisibilityconverter}, converterparameter=admin}"/> 

i error in customprincipal.cs _identity null. how can fix this?

loginviewmodel

[export(typeof(loginviewmodel))] public class loginviewmodel : propertychangedbase, iviewaware, idataerrorinfo {     private readonly iwindowmanager _windowmanager;     private readonly iauthenticationservice _authenticationservice;      [importingconstructor]     public loginviewmodel(iwindowmanager windowmanager, iauthenticationservice authenticationservice)     {         _windowmanager = windowmanager;         _authenticationservice = authenticationservice;     }      public loginviewmodel() { }      private string _username;     public string username     {         { return _username; }         set         {             _username = value;             notifyofpropertychange(() => username);         }     }      public string authenticateduser     {                 {             if (isauthenticated)                 return string.format("signed in {0}. {1}",                       thread.currentprincipal.identity.name,                       thread.currentprincipal.isinrole("administrators") ? "you administrator!"                           : "you not member of administrators group.");              return "not authenticated!";         }     }      private string _status;     public string status     {         { return _status; }         set         {             _status = value;             notifyofpropertychange(() => status);         }     }      public void login(object parameter)     {         var passwordbox = parameter passwordbox;         var cleartextpassword = passwordbox.password;          try         {             //validate credentials through authentication service             var user = _authenticationservice.authenticateuser(username, cleartextpassword);              var customprincipal = thread.currentprincipal customprincipal;             if (customprincipal == null)             {                 throw new argumentexception("the application's default thread principal must set customprincipal object on startup.");             }              customprincipal.identity = new customidentity(user.username, user.roles);              notifyofpropertychange(() => authenticateduser);             notifyofpropertychange(() => isauthenticated);             executecancelcommand();         }         catch (unauthorizedaccessexception)         {             status = "login failed! please provide valid credentials.";         }         catch (exception ex)         {             status = string.format("an error occured: {0}", ex.message);         }     }      public bool canlogin(object parameter)     {         return !isauthenticated;     }      public bool isauthenticated     {         { return thread.currentprincipal.identity.isauthenticated; }     }       /*close view */      public void executecancelcommand()     {         loginwindow.close();     }      private window loginwindow;     public void attachview(object view, object context = null)     {         loginwindow = view window;         viewattached?.invoke(this, new viewattachedeventargs() { context = context, view = view });     }      public object getview(object context = null)     {         return loginwindow;     }      public event eventhandler<viewattachedeventargs> viewattached;      /* required fields handling */      public string error     {         { return null; }     }      public string this[string columnname]     {                 {             switch (columnname)             {                 case "username":                     if (string.isnullorempty(username))                         return "username required.";                     break;             }              return string.empty;         }     }  } 

roletovisibilityconverter

public class roletovisibilityconverter : ivalueconverter {     public object convert(object value, type targettype, object parameter, cultureinfo culture)     {         var principal = value customprincipal;         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();     } } 

customprincipal

public class customprincipal : iprincipal {     private customidentity _identity;      public customidentity identity     {         { return _identity ?? new anonymousidentity(); }         set { _identity = value; }     }      #region iprincipal members     iidentity iprincipal.identity     {         { return this.identity; }     }      public bool isinrole(string role)     {         return _identity.roles.contains(role);     }     #endregion } 

app

public partial class app : application {     protected override void onstartup(startupeventargs e)     {         //create custom principal anonymous identity @ startup         var customprincipal = new customprincipal();         appdomain.currentdomain.setthreadprincipal(customprincipal);          base.onstartup(e);     } } 


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -