3

During initializing my application I set the System.Threading.Thread.CurrentPrincial. But after the application is initialized and I want to access to this Principal, the CurrentPrincipal contains again the default GenericPrincipal.

Anyone an idea why i get this behavior? And how I have to set the Principal for accessing to it after the application is initialized.

Following example demonstrates the behavior:

MainWindow.xaml:

<Window x:Class="PrincipalTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
   <Grid>
       <Button Click="ButtonClick"/>
   </Grid>
</Window>

MainWindow.xaml.cs:

using System.Collections.Generic;
using System.Diagnostics;
using System.Security.Claims;
using System.Threading;
using System.Windows;

namespace PrincipalTest
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Debug.WriteLine("Principal Type: {0}", Thread.CurrentPrincipal.GetType()); // Principal Type: System.Security.Principal.GenericPrincipal
            Thread.CurrentPrincipal = new ClaimsPrincipal();
            Debug.WriteLine("Principal Type: {0}", Thread.CurrentPrincipal.GetType()); // Principal Type: System.Security.Claims.ClaimsPrincipal
        }

        private void ButtonClick(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("Principal Type: {0}", Thread.CurrentPrincipal.GetType()); // Principal Type: System.Security.Principal.GenericPrincipal
        }
    }
}

In my real project I set the Thread.CurrentPrincipal property in the Bootstrapper.

rhe1980
  • 1,537
  • 1
  • 14
  • 34
  • What are you trying to achieve? User impersonation? – Vladimir Gondarev Apr 17 '14 at 06:48
  • in the bootstrapper I want to set the currentPrinciple with a claim which contains the username. This username I retrieve from Login-Gui, ActiveDirectory-User or Database. In case of a Login-Gui there is no problem, and the CurrentPrincipal isn't reset to GenericPrincipal – rhe1980 Apr 17 '14 at 06:55
  • 3
    In order to override the Current Principal in WPF you have to use the AppDomain.CurrentDomain.SetThreadPrincipal(new ClaimsPrincipal()); – Vladimir Gondarev Apr 17 '14 at 07:17
  • @VladimirGondarev: This works fine. Thanks for your answer. – rhe1980 Apr 17 '14 at 09:40
  • I have the same problem and I am also using Caliburn Micro. Have you found out why this is happening? – DELUXEnized Oct 02 '14 at 15:28

1 Answers1

4

You have to use AppDomain.CurrentDomain.SetThreadPrincipal()

The CurrentPrincipal is reset to default (GenericPrincipal) after Window.Loaded

Steven Muhr
  • 3,181
  • 23
  • 43
  • Thanks, FINALLY. Your last statement fixed it for me "AFTER Window.Loaded". Sheesh. I have a little hair left. – Bruce Pierson Mar 02 '16 at 05:07
  • Also beware that if you first call the getter for `Thread.CurrentPrincipal.Identity`, and later `AppDomain.CurrentDomain.SetThreadPrincipal()`, then it won't work either. `AppDomain.CurrentDomain.SetThreadPrincipal()` should always be the first statement. – sventevit Jan 18 '19 at 12:07