简体   繁体   中英

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.

You have to use AppDomain.CurrentDomain.SetThreadPrincipal()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM