Active Directory とローカル マシン アカウントの両方で動作する必要がある WPF アプリケーションのアクセス許可システムを作成しています。システムがネットワークに接続されている場合と接続されていない場合があります。
ユーザーは、AD ログインまたはローカル マシン ログインのいずれかを使用してアプリケーションにログインできます。アプリケーションはデータを SQL Server Express データベースに格納し、各行には「所有者」がいます。
アプリケーションは、ログインしたユーザーが「所有」している行のみを表示します。
この質問に従って、ユーザーを識別するために保存する推奨データは LDAP ですobjectGUID
。これは、以下のコードで取得し、uniqueidentifier 列に保存できます。
using System.DirectoryServices.AccountManangement;
using System.Security.Principal;
public class ActiveDirectoryHelper
{
public static Guid? GetObjectGuidFromIdentity(WindowsIdentity identity)
{
string domainName = identity.Name.Split('\\')[0];
PrincipalContext pc = null;
if (domainName == Environment.MachineName)
{
pc = new PrincipalContext(ContextType.Machine);
}
else
{
pc = new PrincipalContext(ContextType.Domain, domainName);
}
UserPrincipal user = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, identity.Name);
return user.Guid;
}
}
ただし、UserPrincipal.Guid
は null ですContextType.MachineName
。
AD アカウントまたはローカル アカウントのいずれかを参照できる、保存できる単一の情報はありますか?
または、別の列を追加してディレクトリの種類 (AD/SAM) を指定し、別の列を使用して別の識別子 (SID) を格納する必要がありますか?