在Thread.CurrentPrincipal中.NET控制台应用程序 [英] Thread.CurrentPrincipal in .NET console application

查看:155
本文介绍了在Thread.CurrentPrincipal中.NET控制台应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

请参阅<一href="http://msdn.microsoft.com/en-us/library/system.appdomain.setprincipalpolicy.aspx">http://msdn.microsoft.com/en-us/library/system.appdomain.setprincipalpolicy.aspx

这篇关于在Thread.CurrentPrincipal中.NET控制台应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆