为什么 WindowsIdentity 类在 .NET Core 中不可见 [英] Why WindowsIdentity class is not visible in .NET Core

查看:35
本文介绍了为什么 WindowsIdentity 类在 .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 Core 文档 ?

相同的代码适用于 .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, Version=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?

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

我还在 那个链接

我是不是找错地方了?

推荐答案

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

Microsoft announced Windows Compatibility Pack for .NET Core a few weeks ago,

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

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

And by analyzing the source code of System.Security.Principal.Windows.csproj and the commit adding it,

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

我的结论是,这也是 Windows only 兼容库的一部分,所以只能在 Windows 上使用.

My conclusion is that this is also part of the Windows only compatibility libraries, so can only be used on Windows.

要将其添加到您的项目中,请打开您的 csproj 并手动为 System.Security.Principal.Windows 添加一个 PackageReference 标记(或使用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆