ActiveDirectoryから拡張プロパティを取得しようとしています。だから私は以下のような追加のクラスを持っています:
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?)
わかりました-今朝飲んだコーヒーが足りません-UserPrincipalExクラスに自分のバージョンのFindByIdentityを実装するのを忘れました。
public static new UserPrincipalEx FindByIdentity(PrincipalContext context,
string identityValue)
{
return (UserPrincipalEx)FindByIdentityWithType(context,
typeof(UserPrincipalEx),
identityValue);
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加