在Thread.CurrentPrincipal中.NET控制台应用程序控制台、应用程序、Thread、CurrentPrincipal

2023-09-03 15:53:04 作者:北巷思婳雨ゝ

下面是我在运行命令提示符下一个微不足道的控制台应用程序:

Here is a trivial console application that i run in command prompt:

using System;
using System.Threading;
namespace Test
{
    internal class Runner
    {
        [STAThread]
        static void Main(string[] args)
        {
            Console.WriteLine(Thread.CurrentPrincipal.GetType().Name);
            Console.WriteLine(Thread.CurrentPrincipal.Identity.Name);
        }
    }
}

输出是'的GenericPrincipal'和空字符串作为标识名称。为什么运行时的结构的GenericPrincipal ,而不是的WindowsPrincipal 的?如何强制它来构造的WindowsPrincipal 从起动过程(cmd.exe,在我的情况)?

The output is 'GenericPrincipal' and empty string as identity name. Why the run-time constructs GenericPrincipal instead of WindowsPrincipal? How do i force it to construct WindowsPrincipal from the security token of the starting process (cmd.exe in my case)?

推荐答案

您必须告诉您的应用程序使用什么PrincipalPolicy。你会添加

You have to tell your app what PrincipalPolicy to use. You would add

AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);

让你的code样子:

making your code look like:

using System;
using System.Threading;
using System.Security.Principal;

namespace Test
{
    internal class Runner
    {
        [STAThread]
        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
            Console.WriteLine(Thread.CurrentPrincipal.GetType().Name);
            Console.WriteLine(Thread.CurrentPrincipal.Identity.Name);
        }
    }
}

请参阅http://msdn.microsoft.com/en-us/library/system.appdomain.setprincipalpolicy.aspx

 
精彩推荐
图片推荐