为什么 WindowsIdentity 类在 .NET Core 中不可见

2024-01-03

在 Visual Studio 2017 .NET Core 2.0 控制台应用程序中包含以下代码

using System;
using System.Security.Principal;

namespace smallTests
{
    class Program
    {
        static void Main(string[] args)
        {
            var identity = WindowsIdentity.GetCurrent();
        }
    }
}

为什么我收到错误:

The name 'WindowsIdentity' does not exist in the current context    

如果我可以在 .NET Core 2.0 库中看到这个类.Net Core 文档 https://learn.microsoft.com/en-us/dotnet/api/system.security.principal.windowsidentity?view=netcore-2.0 ?

相同的代码适用于 .NET 控制台应用程序。

[EDIT]

@威尔@JohnnyL评论说我不参考,System.Security.Principal.Windows.dll, 那是真实的。

但我很好奇为什么它不起作用,因为 在.NET 4.6.1项目中(其中类WindowsIdentity可见)我也不引用这个System.Security.Principal.Windows.dll具体来说。不过我指的是System.dll。

我一直认为它的工作原理类似于命名空间层次结构。例如,当我提到

System.Security.Principal.dll

我可以使用其中的课程

System.Security.Principal.Windows.dll.

我错了吗?

I added System.Security.Principal.dll手动到.NetCore解决方案,但它仍然不起作用。

[EDIT2]

@Will非常感谢您对这个主题的解释,它对我帮助很大。 我试图弄清楚 WindowsIdentity 是否与 Core 兼容,看起来是这样,请参阅:

in this apisof.net https://apisof.net/catalog/System.Security.Principal.WindowsIdentity在声明区域我可以看到 WindowsIdentity 位于.Net Core 2.0 System.Security.Principal.Windows, Version=4.1.1.0, PublicKeyToken=b03f5f7f11d50a3a但是我没有System.Security.Principal.Windows.dll在参考文献中,我应该添加它吗?如果是的话从哪里来?

in 我在列表中看到这个类(如果它与核心不兼容,该列表的目的是什么?

我还找到了有关该课程的信息那个链接 https://source.dot.net/#System.Security.Principal.Windows/System/Security/Principal/WindowsIdentity.cs,200d683bfb324fda

我是不是找错地方了?


微软几周前发布了适用于 .NET Core 的 Windows 兼容包,

https://blogs.msdn.microsoft.com/dotnet/2017/11/16/announcing-the-windows-compatibility-pack-for-net-core/ https://blogs.msdn.microsoft.com/dotnet/2017/11/16/announcing-the-windows-compatibility-pack-for-net-core/

并且通过分析源码System.Security.Principal.Windows.csproj以及添加它的提交,

https://github.com/dotnet/corefx/blob/master/src/System.Security.Principal.Windows/src/System.Security.Principal.Windows.csproj https://github.com/dotnet/corefx/blob/master/src/System.Security.Principal.Windows/src/System.Security.Principal.Windows.csproj

我的结论是,这也是Windows唯一兼容库的一部分,因此只能在Windows上使用。

要将其添加到您的项目中,请打开您的 csproj 并添加PackageReference标记为System.Security.Principal.Windows手动(或使用 Visual Studio 的 NuGet 包管理器)。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么 WindowsIdentity 类在 .NET Core 中不可见 的相关文章

随机推荐