为什么WindowsIdentity类在.NET Core中不可见 [英] Why WindowsIdentity class is not visible in .NET Core
问题描述
在VisualStudio 2017 .NET Core 2.0控制台应用程序中具有以下代码
Having the code below in VisualStudio 2017 .NET Core 2.0 Console App
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核心文档?
相同的代码在.NET控制台应用程序。
Same code works in .NET Console app.
@Will @JohnnyL 评论我不引用 System.Security.Principal.Windows.dll
,这是正确的。
@Will @JohnnyL Commented that I do not refer, System.Security.Principal.Windows.dll
, that is true.
但是我很好奇为什么它不起作用,因为.NET 4.6.1项目(可见WindowsIdentity的类)中的
我也没有引用 System.Security.Principal.Windows.dll
专门。但是我指的是System.dll。
But I am curious why it is not working, because
in .NET 4.6.1 project (where class WindowsIdentity is visible) I also do not refer this System.Security.Principal.Windows.dll
specifically. However i refer System.dll.
我一直认为它像命名空间层次结构一样工作。例如,当我引用
I always thought that it works like namespace hierarchy. For instance, when I refer to
System.Security.Principal.dll
我可以使用
System.Security.Principal.Windows.dll
。
我错了吗?
我添加了 System.Security.Principal.dll
手动添加到.NetCore解决方案,但仍然无法使用。
I added System.Security.Principal.dll
to .NetCore solution by hand but it still does not work.
@Will 非常感谢您对这个主题有很大的帮助。我试图找出与WindowsIdentity兼容的
与Core兼容,似乎请参见:
@Will Thank you a lot for expaining the subject it helped me a lot. I tried to figure out is WindowsIdentity compatible with Core and it seems that it is please see:
在此 apisof.net 我可以看到WindowsIdentity位于 .Net Core 2.0 System.Security中。 Principal.Windows,版本= 4.1.1.0,PublicKeyToken = b03f5f7f11d50a3a
,但是我没有 System.Security.Principal.Windows.dll
在参考文献中,我应该添加它吗?如果是从哪里来?
in this apisof.net in Declarations area i can see that WindowsIdentity is in .Net Core 2.0 System.Security.Principal.Windows, Version=4.1.1.0, PublicKeyToken=b03f5f7f11d50a3a
but i do not have System.Security.Principal.Windows.dll
in references, should I add it? If yes from where?
& term = WindowsIdentity rel = noreferrer>。NET Core api参考我在列表中看到此类(该列表与核心不兼容的目的是什么?
in .NET Core api reference i see this class in the list (what is the purpose of that listing if it is not compatible with core?
我还在该链接
我看错地方了吗?
推荐答案
Microsoft几周前宣布了.NET Core的Windows兼容包,
Microsoft announced Windows Compatibility Pack for .NET Core a few weeks ago,
并通过分析源代码的 System.Security.Principal.Windows.csproj
并添加提交,
And by analyzing the source code of System.Security.Principal.Windows.csproj
and the commit adding it,
我的结论是,这也是其中的一部分Windows的唯一兼容性库,因此只能在Windows上使用。
My conclusion is that this is also part of the Windows only compatibility libraries, so can only be used on Windows.
要将其添加到项目中,请打开csproj并添加 PackageReference
标签,用于手动 System.Security.Principal.Windows
(或使用Visual Studio的NuGet包管理器)。
To add that to your project, open your csproj and add a PackageReference
tag for System.Security.Principal.Windows
manually (or use Visual Studio's NuGet Package Manager).
这篇关于为什么WindowsIdentity类在.NET Core中不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!