[API Proposal]: Adding a generic version of `FindByIdentity`

Open
#116,295 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
38/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Quiet
Tech stack
csharp

Research direction

Start by locating Principal.FindByIdentity, the protected FindByIdentityWithType implementation, and the ComputerPrincipal, GroupPrincipal, and UserPrincipal declarations. Compare the proposed generic overloads with the existing API surface and determine the compatibility and validation requirements; done means the proposal is implemented consistently and the shown generic usage works.

Written by the indexing model from the issue text.

Description

api-suggestion area-System.DirectoryServices feature-request
Background and motivation

There is currently no way to use FindByIdentity in a generic way. There is a protected function FindByIdentityWithType, but this is not accessible from "outside".

We have functions that have to call FindByIdentity with different derived Principal classes. This currently causes us to call FindByIdentity as follows:

var result = typeof(T).GetMethod(nameof(Principal.FindByIdentity), [typeof(PrincipalContext), typeof(IdentityType), typeof(string)])?.Invoke(null, [adPrincipalContext, identityType, identityValue]) as T;

I think that due to the existence of FindByIdentityWithType, the implementation should also be straightforward and should not cause any problems.

API Proposal
namespace System.DirectoryServices.AccountManagement;

public abstract partial class Principal : System.IDisposable
{
    public static new T FindByIdentity<T>(PrincipalContext context, string identityValue) where T : Principal
    public static new T FindByIdentity<T>(PrincipalContext context, IdentityType identityType, string identityValue) where T : Principal
}

public partial class ComputerPrincipal : System.DirectoryServices.AccountManagement.AuthenticablePrincipal
{
    public static new T FindByIdentity<T>(PrincipalContext context, string identityValue) where T : ComputerPrincipal
    public static new T FindByIdentity<T>(PrincipalContext context, IdentityType identityType, string identityValue) where T : ComputerPrincipal
}

public partial class GroupPrincipal : System.DirectoryServices.AccountManagement.Principal
{
    public static new T FindByIdentity<T>(PrincipalContext context, string identityValue) where T : GroupPrincipal
    public static new T FindByIdentity<T>(PrincipalContext context, IdentityType identityType, string identityValue) where T : GroupPrincipal
}

public partial class UserPrincipal : System.DirectoryServices.AccountManagement.AuthenticablePrincipal
{
    public static new T FindByIdentity<T>(PrincipalContext context, string identityValue) where T : UserPrincipal
    public static new T FindByIdentity<T>(PrincipalContext context, IdentityType identityType, string identityValue) where T : UserPrincipal
}
API Usage
PrincipalContext ctx = new PrincipalContext(ContextType.Machine);
Principal.FindByIdentity<UserPrincipal>(ctx, IdentityType.SamAccountName, "Guest");
Alternative Designs

No response

Risks

No response

Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/runtime

All issues in dotnet/runtime

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.