我正在尝试从 Active Directory 获取扩展属性。所以我有一个额外的课程如下:
namespace MyProject.Web.Utility
[DirectoryRdnPrefix("CN")]
[DirectoryObjectClass("Person")]
public class UserPrincipalEx : UserPrincipal
{
// Inplement the constructor using the base class constructor.
public UserPrincipalEx(PrincipalContext context)
: base(context)
{ }
// Implement the constructor with initialization parameters.
public UserPrincipalEx(PrincipalContext context,
string samAccountName,
string password,
bool enabled)
: base(context, samAccountName, password, enabled)
{ }
// Create the "extensionAttribute2" property.
[DirectoryProperty("extensionAttribute2")]
public string ExtensionAttribute2
{
get
{
if (ExtensionGet("extensionAttribute2").Length != 1)
return string.Empty;
return (string)ExtensionGet("extensionAttribute2")[0];
}
set { ExtensionSet("extensionAttribute2", value); }
}
}
我试图在另一个类(在同一个命名空间中)中使用它,如下所示:
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
// Search the directory for the new object.
UserPrincipalEx inetPerson = UserPrincipalEx.FindByIdentity(ctx, identityName);
string test = inetPerson.ExtensionAttribute2;
// you can easily access the ExtensionAttribute2 now
string department = inetPerson.ExtensionAttribute2;
}
但是 - 这会引发以下错误 - 我尝试将 UserPrincipalEx 转换为 UserPrincipal 但这也不起作用 - 我还尝试调用 base.FindByIdentity 但它不起作用 - 我该如何解决这个问题?
Error 81 Cannot implicitly convert type 'System.DirectoryServices.AccountManagement.UserPrincipal' to 'MyProj.Web.Utility.UserPrincipalEx'. An explicit conversion exists (are you missing a cast?)