加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 为什么在.NET Core中看不到WindowsIdentity类

发布时间:2020-12-15 04:10:46 所属栏目:百科 来源:网络整理
导读:在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(); } }} 为什么我收
在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找到有关该课程的信息

我在错误的地方看?

解决方法

微软几周前发布了针对.NET Core的Windows兼容包,

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读