rhe1980
rhe1980

Reputation: 1577

Thread.CurrentPrincipal is reset to default GenericPrincipal

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.

Upvotes: 4

Views: 2337

Answers (1)

Steven Muhr
Steven Muhr

Reputation: 3379

You have to use AppDomain.CurrentDomain.SetThreadPrincipal()

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

Upvotes: 4

Related Questions