繁体   English   中英

Thread.CurrentPrincipal重置为默认GenericPrincipal

[英]Thread.CurrentPrincipal is reset to default GenericPrincipal

在初始化我的应用程序期间,我设置了System.Threading.Thread.CurrentPrincial 但是在初始化应用程序并且我想要访问此Principal之后,CurrentPrincipal再次包含默认的GenericPrincipal。

任何人都知道我为什么会这样做? 以及如何在初始化应用程序后设置Principal以访问它。

以下示例演示了该行为:

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
        }
    }
}

在我的实际项目中,我在Bootstrapper中设置了Thread.CurrentPrincipal属性。

你必须使用AppDomain.CurrentDomain.SetThreadPrincipal()

在Window.Loaded之后,CurrentPrincipal重置为默认值(GenericPrincipal)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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