UserPrincipal.FindByIdentity Method (PrincipalContext, IdentityType, String)
Returns a user principal object that matches the specified identity type, and value. This version of the FindByIdentity method determines the format of the identity value.
Namespace: System.DirectoryServices.AccountManagement
Assembly: System.DirectoryServices.AccountManagement (in System.DirectoryServices.AccountManagement.dll)
public static UserPrincipal FindByIdentity( PrincipalContext context, IdentityType identityType, string identityValue )
Parameters
- context
- Type: System.DirectoryServices.AccountManagement.PrincipalContext
The PrincipalContex that specifies the server or domain against which operations are performed.
- identityType
- Type: System.DirectoryServices.AccountManagement.IdentityType
A IdentityType enumeration value that specifies the format of the identityValue parameter.
- identityValue
- Type: System.String
The identity of the user principal. This parameter can be any format that is contained in the IdentityType enumeration.
Return Value
Type: System.DirectoryServices.AccountManagement.UserPrincipalA UserPrincipal object that matches the specified identity value and type, or null if no matches are found.
| Exception | Condition |
|---|---|
| MultipleMatchesException | Multiple user principal objects matching the current user object were found. |
| InvalidEnumerationOperatorException | The identity value is not a valid IdentityType enumeration value. |
The following code connects to the local computer store. Since the credentials are not specified in the PrincipalContext constructor, the credentials of the user running the current thread are used.
A search is performed to find the user who has SamAccountName "Guest".
If the user is found, a check is performed to determine whether this user's account is enabled. If the account is not enabled, the example code enables it.
PrincipalContext ctx = new PrincipalContext(ContextType.Machine);
UserPrincipal usr = UserPrincipal.FindByIdentity(ctx,
IdentityType.SamAccountName,
"Guest");
if(usr != null)
{
if (usr.Enabled == false)
usr.Enabled = true;
usr.Save();
usr.Dispose();
}
ctx.Dispose();
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.