Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why WindowsIdentity class is not visible in .NET Core

Tags:

c#

.net

.net-core

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();
        }
    }
}

Why am I getting the error:

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

If I can see this class in .NET Core 2.0 library in .Net Core docs ?

Same code works in .NET Console app.

[EDIT]

@Will @JohnnyL Commented that I do not refer, System.Security.Principal.Windows.dll, that is true.

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

i can use class which is in

System.Security.Principal.Windows.dll.

Am I wrong?

I added System.Security.Principal.dll to .NetCore solution by hand but it still does not work.

[EDIT2]

@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:

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?

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?

I also find information about that class in that link

Am I looking in wrong places?

like image 643
Reven Avatar asked Dec 12 '17 15:12

Reven


1 Answers

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

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

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

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

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

like image 160
Lex Li Avatar answered Nov 09 '22 16:11

Lex Li