To connect to an ldap-Server i want to use Directory Services in a c#-console application.
- The services are available in dotnet-7.0
- Therefore i need to upgrade from dotnet-5.0
These are the steps to reproduce:
- On a console execute these commands
dotnet new console -f=net5.0 -o=ConsoleFiveToSeven
cd ConsoleFiveToSeven
dotnet run
- Inside
ConsoleFiveToSeven.csproj
change<TargetFramework>net5.0</TargetFramework>
to<TargetFramework>net7.0</TargetFramework>
and save - Execute
dotnet run
again. This createsConsoleFiveToSevenbinDebugnet7.0
- Execute
dotnet add package System.DirectoryServices.Protocols --version 7.0.0
- Add the code below
- Execute
dotnet build
What is the problem?
- Expected: The solutions compiles without a problem
- Actual: Error CS0103
distinguishedName
andldapFilter
andsearchScope
andattributeList
does not exist in the current context
The docs for SearchRequest contains this constructor
public SearchRequest (string distinguishedName
, string ldapFilter
, System.DirectoryServices.Protocols.SearchScope searchScope
, params string[] attributeList);
Question
To my understanding the following code below should be fine.
What is the problem?
Code for step 8 above
using System;
using System.Net;
using System.DirectoryServices.Protocols;
namespace ConsoleFiveToSeven
{
class Program
{
static void Main(string[] args)
{
SearchRequest search = new SearchRequest(
distinguishedName = "foo",
ldapFilter = "foo",
searchScope = SearchScope.Subtree,
attributeList = new string [] {"cn=foo", "cn=bar", "cn=com"}
);
}
}
}
Not entirely. In C#, =
to specify parameters by names is invalid. You should use :
SearchRequest search = new SearchRequest(
distinguishedName: "foo",
ldapFilter: "foo",
searchScope: SearchScope.Subtree,
attributeList: new string [] {"cn=foo", "cn=bar", "cn=com"});
But if you use default order of parameters you can omit their names:
SearchRequest search = new SearchRequest(
"foo",
"foo",
SearchScope.Subtree,
new string [] {"cn=foo", "cn=bar", "cn=com"});
1