这是一个简单的控制台应用程序,我在命令提示符下运行:
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)?
答案 0 :(得分:32)
您必须告诉您的应用使用哪种PrincipalPolicy。你会添加
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
使您的代码看起来像:
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