Home > Software design >  The type or namespace name 'PrincipalContext' could not be found
The type or namespace name 'PrincipalContext' could not be found

Time:07-13

When attempting to run below C# code in VSCode, I got the following the error message. How can I solve this?

Error message:

[{
    "resource": "/c:/C#/app/Program.cs",
    "owner": "csharp",
    "code": "CS0246",
    "severity": 8,
    "message": "The type or namespace name 'PrincipalContext' could not be found (are you missing a using directive or an assembly reference?) [app]",
    "source": "csharp",
    "startLineNumber": 9,
    "startColumn": 29,
    "endLineNumber": 9,
    "endColumn": 45
},{
    "resource": "/c:/C#/app/Program.cs",
    "owner": "csharp",
    "code": "CS0103",
    "severity": 8,
    "message": "The name 'ContextType' does not exist in the current context [app]",
    "source": "csharp",
    "startLineNumber": 9,
    "startColumn": 46,
    "endLineNumber": 9,
    "endColumn": 57
},{
    "resource": "/c:/C#/app/Program.cs",
    "owner": "csharp",
    "code": "CS0103",
    "severity": 8,
    "message": "The name 'GroupPrincipal' does not exist in the current context [app]",
    "source": "csharp",
    "startLineNumber": 11,
    "startColumn": 19,
    "endLineNumber": 11,
    "endColumn": 33
},{
    "resource": "/c:/C#/app/Program.cs",
    "owner": "csharp",
    "code": "CS0246",
    "severity": 8,
    "message": "The type or namespace name 'PrincipalContext' could not be found (are you missing a using directive or an assembly reference?) [app]",
    "source": "csharp",
    "startLineNumber": 13,
    "startColumn": 33,
    "endLineNumber": 13,
    "endColumn": 49
},{
    "resource": "/c:/C#/app/Program.cs",
    "owner": "csharp",
    "code": "CS0103",
    "severity": 8,
    "message": "The name 'ContextType' does not exist in the current context [app]",
    "source": "csharp",
    "startLineNumber": 13,
    "startColumn": 50,
    "endLineNumber": 13,
    "endColumn": 61
},{
    "resource": "/c:/C#/app/Program.cs",
    "owner": "csharp",
    "code": "CS0103",
    "severity": 8,
    "message": "The name 'IdentityType' does not exist in the current context [app]",
    "source": "csharp",
    "startLineNumber": 15,
    "startColumn": 36,
    "endLineNumber": 15,
    "endColumn": 48
}]

Code

string UserDomain = Environment.UserDomainName.ToString();
string UserName = Environment.UserName.ToString();
Console.WriteLine(UserDomain);
Console.WriteLine(Environment.UserName);


try
{
   using (var pcLocal = new PrincipalContext(ContextType.Machine))
   {
      var group = GroupPrincipal.FindByIdentity(pcLocal, "Administrators");

      using (var pcDomain = new PrincipalContext(ContextType.Domain, "UserDomain"))
      {
       group.Members.Add(pcDomain, IdentityType.SamAccountName, "UserName");                            
         group.Save();
      };
   };
} catch (Exception e)
{
   Console.WriteLine(e.Message);
};

app.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="System.DirectoryServices" Version="7.0.0-preview.6.22324.4" />
    <PackageReference Include="System.DirectoryServices.AccountManagement" Version="7.0.0-preview.6.22324.4" />
  </ItemGroup>

</Project>

CodePudding user response:

PrincipalContext is in System.DirectoryServices.AccountManagement namespace so beside installing the package (which you appear to have done correctly), you need to declare that you are using that namespace as well:

// Either put this on top:
using System.DirectoryServices.AccountManagement

// Or use fully qualified name:
using (var pcLocal = new System.DirectoryServices.AccountManagement.PrincipalContext(ContextType.Machine))

A side note: you can also use this using statement since you wrap it for the whole code block:

using var pcLocal = new PrincipalContext(ContextType.Machine);
  •  Tags:  
  • c#
  • Related