【发布时间】:2017-12-12 15:57:05
【问题描述】:
在 VisualStudio 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 docs 的 .NET Core 2.0 库中看到此类?
相同的代码适用于 .NET 控制台应用程序。
[编辑]
@Will @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。
我错了吗?
我手动将System.Security.Principal.dll 添加到 .NetCore 解决方案,但它仍然不起作用。
[EDIT2]
@Will 非常感谢您对主题的阐述,它对我帮助很大。 我试图弄清楚 WindowsIdentity 是否与 Core 兼容,它似乎是请参阅:
在声明区域的 apisof.net 中,我可以看到 WindowsIdentity 位于 .Net Core 2.0 System.Security.Principal.Windows, Version=4.1.1.0, PublicKeyToken=b03f5f7f11d50a3a
但我的参考文献中没有System.Security.Principal.Windows.dll,我应该添加它吗?如果是从哪里来的?
在.NET Core api reference 我在列表中看到了这个类(如果它与核心不兼容,那么该列表的目的是什么?
我还在that link中找到有关该课程的信息
我找错地方了吗?
【问题讨论】:
-
WAG,但您可能没有引用 System.Security.Principal.Windows.dll。
-
在文档中有一个字符串
Assembly:,该类所在的程序集 -
@Will 和 JohnyL 谢谢你的提示,请看我的编辑。
-
4.6.1 不是核心。完整框架中的 IIRC WindowsIdentity 存在于 mscorlib 中(已检查,确实如此)。 Core 不引用与完整框架相同的程序集。类型在程序集中定义。如果类型 A 在程序集 Foo.dll 中,则必须引用 Foo.dll 才能使用 A。您需要检查您正在设计的框架的文档(在您的情况下为 .net 核心)并检查以查看哪个程序集定义了给定的类型。此信息有时也包含在快速操作和重构灯泡中...
-
@我会添加 Edit2