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

发布于 2024-10-07 09:17:40 字数 557 浏览 0 评论 0原文

这是我在命令提示符下运行的一个简单的控制台应用程序:

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?我如何强制它从启动进程的安全令牌(在我的例子中为 cmd.exe)构造 WindowsPrincipal

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

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)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江南月 2024-10-14 09:17:40

您必须告诉您的应用程序要使用什么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

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

AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);

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

See http://msdn.microsoft.com/en-us/library/system.appdomain.setprincipalpolicy.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文