Skip to content

[API Proposal]: Adding a generic version of FindByIdentity #116295

Open
@AleMcKee

Description

@AleMcKee

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-System.DirectoryServicesuntriagedNew issue has not been triaged by the area owner

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions