UserPrincipal.FindByIdentity 方法
定义
返回与指定标识值相匹配的用户主体对象。Returns a user principal object that matches the specified identity value.
重载
| FindByIdentity(PrincipalContext, IdentityType, String) |
返回与指定标识类型相匹配的用户主体对象和值。Returns a user principal object that matches the specified identity type, and value. 此版本的 FindByIdentity 方法确定标识值的格式。This version of the FindByIdentity method determines the format of the identity value. |
| FindByIdentity(PrincipalContext, String) |
返回与指定标识值相匹配的用户主体对象。Returns a user principal object that matches the specified identity value. |
FindByIdentity(PrincipalContext, IdentityType, String)
返回与指定标识类型相匹配的用户主体对象和值。Returns a user principal object that matches the specified identity type, and value. 此版本的 FindByIdentity 方法确定标识值的格式。This version of the FindByIdentity method determines the format of the identity value.
public:
static System::DirectoryServices::AccountManagement::UserPrincipal ^ FindByIdentity(System::DirectoryServices::AccountManagement::PrincipalContext ^ context, System::DirectoryServices::AccountManagement::IdentityType identityType, System::String ^ identityValue);
public static System.DirectoryServices.AccountManagement.UserPrincipal FindByIdentity (System.DirectoryServices.AccountManagement.PrincipalContext context, System.DirectoryServices.AccountManagement.IdentityType identityType, string identityValue);
static member FindByIdentity : System.DirectoryServices.AccountManagement.PrincipalContext * System.DirectoryServices.AccountManagement.IdentityType * string -> System.DirectoryServices.AccountManagement.UserPrincipal
Public Shared Function FindByIdentity (context As PrincipalContext, identityType As IdentityType, identityValue As String) As UserPrincipal
参数
- context
- PrincipalContext
PrincipalContext,用于指定要对其执行操作的服务器或域。The PrincipalContext that specifies the server or domain against which operations are performed.
- identityType
- IdentityType
IdentityType 枚举值,用于指定 identityValue 参数的格式。A IdentityType enumeration value that specifies the format of the identityValue parameter.
- identityValue
- String
用户主体的标识。The identity of the user principal. 此参数的格式可以是 IdentityType 枚举中包含的任何格式。This parameter can be any format that is contained in the IdentityType enumeration.
返回
与指定标识值和类型相匹配的 UserPrincipal 对象;若未找到匹配,则为 null。A UserPrincipal object that matches the specified identity value and type, or null if no matches are found.
例外
已找到多个与当前用户对象相匹配的用户主体对象。Multiple user principal objects matching the current user object were found.
该标识值不是有效的 IdentityType 枚举值。The identity value is not a valid IdentityType enumeration value.
示例
以下代码连接到本地计算机存储。The following code connects to the local computer store. 由于未在构造函数中指定凭据 PrincipalContext ,因此将使用运行当前线程的用户的凭据。Since the credentials are not specified in the PrincipalContext constructor, the credentials of the user running the current thread are used.
执行搜索以查找具有 SamAccountName "Guest" 的用户。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();
适用于
FindByIdentity(PrincipalContext, String)
返回与指定标识值相匹配的用户主体对象。Returns a user principal object that matches the specified identity value.
public:
static System::DirectoryServices::AccountManagement::UserPrincipal ^ FindByIdentity(System::DirectoryServices::AccountManagement::PrincipalContext ^ context, System::String ^ identityValue);
public static System.DirectoryServices.AccountManagement.UserPrincipal FindByIdentity (System.DirectoryServices.AccountManagement.PrincipalContext context, string identityValue);
static member FindByIdentity : System.DirectoryServices.AccountManagement.PrincipalContext * string -> System.DirectoryServices.AccountManagement.UserPrincipal
Public Shared Function FindByIdentity (context As PrincipalContext, identityValue As String) As UserPrincipal
参数
- context
- PrincipalContext
PrincipalContext,用于指定要对其执行操作的服务器或域。The PrincipalContext that specifies the server or domain against which operations are performed.
- identityValue
- String
用户主体的标识。The identity of the user principal. 此参数的格式可以是 IdentityType 枚举中包含的任何格式。This parameter can be any format that is contained in the IdentityType enumeration.
返回
与指定标识值相匹配的 UserPrincipal 对象,如果找不到匹配项,则为 null。A UserPrincipal object that matches the specified identity value, or null if no matches are found.
例外
已找到多个与当前用户对象相匹配的用户主体对象。Multiple user principal objects matching the current user object were found.