Monday, October 12, 2015

WPF/WinForms: Set the Windows Identity as the CurrentPrincipal

In a WPF application we created(yes, WPF is still alive and kicking), security is based on the Windows Identity people used to login on their systems.

However we noticed that when we checked the current Identity through System.Threading.Thread.CurrentPrincipal.Identity, we got the following back: 

{System.Security.Principal.GenericIdentity}
    [System.Security.Principal.GenericIdentity]: {System.Security.Principal.GenericIdentity}
    AuthenticationType: ""
    IsAuthenticated: false
    Name: ""

To change this, we used the following line of code:

AppDomain.CurrentDomain.SetPrincipalPolicy(System.Security.Principal.PrincipalPolicy.WindowsPrincipal);

After doing that, calling System.Threading.Thread.CurrentPrincipal.Identity returned:

{System.Security.Principal.WindowsIdentity}
    [System.Security.Principal.WindowsIdentity]: {System.Security.Principal.WindowsIdentity}
    AuthenticationType: "Kerberos"
    IsAuthenticated: true
    Name: "ORDINA\\BaWu"

No comments: