关于 c#:如何从 DOMAIN\\user 格式的用户名创建 WindowsIdentity/WindowsPrincipal
How to create WindowsIdentity/WindowsPrincipal from username in DOMAIN\user format
从旧样式 (sAMAccountName) 用户名??创建
相关讨论
- 您可以使用 DsCrackNames API 来转换格式。
- 我在这里找到了一个 DsCrackNames C# 示例:technolog.nl/blogs/eprogrammer/archive/2005/11/16/…2800_upda??te_2900.aspx
似乎没有办法在不涉及对 Active Directory 的查询的情况下转换用户名格式。由于是这种情况,因此无需创建
通过使用
|
string accountName = @"DOMAIN\\user";
var groupNames = new[] {"DOMAIN\\Domain Users","DOMAIN\\Group2" }; // the groups that we need to verify if the user is member of // cannot create WindowsIdentity because it requires username in form user@domain.com but the passed value will be DOMAIN\\user. // if you need just the UPN of the user, you can use this // find all groups the user is member of (the check is recursive). // use a HashSet to find the group the user is member of. return groups; |
相关讨论
- 哪个 .NET 框架版本?和组件?哪个是 AccountManagement、PrincipalContext、UserPrincipal、StringComparer 类?
-
为什么
UserPrincipal 在 using 语句中?它没有Dispose() 方法?! - @Muflix - 它(和任何委托人)确实实现了 IDisposable:docs.microsoft.com/en-us/dotnet/api/...
这可以正常工作,但涉及对活动目录/SAM 存储的查询(取决于上下文)...
|
private WindowsIdentity GetWindowsIdentity(
string userName) { using (var user = UserPrincipal.FindByIdentity( UserPrincipal.Current.Context, IdentityType.SamAccountName, userName ) ?? UserPrincipal.FindByIdentity( UserPrincipal.Current.Context, IdentityType.UserPrincipalName, userName )) { return user == null ? null : new WindowsIdentity(user.UserPrincipalName); } } |
我使用了 pinvoke.net 示例中的 DsCrackNames 并对其进行了修改,以将其从 nt4 名称转换为 UPN。它有点马虎,你可能想清理一下。为此,它也必须击中 DS。他们有 DS_NAME_FLAG_SYNTACTICAL_ONLY 标志,可用于不点击目录,但我认为这不会在这里工作。
|
class Entry
{ const uint NO_ERROR = 0; [DllImport("ntdsapi.dll", CharSet = CharSet.Auto)] public enum DS_NAME_ERROR // Generic processing error. // Couldn't find the name at all - or perhaps caller doesn't have // Input name mapped to more than one output name. // Input name found, but not the associated output format. // Unable to resolve entire name, but was able to determine which // Unable to perform a purely syntactical mapping at the client // The name is from an external trusted forest. } [Flags] // Perform a syntactical mapping at the client (if possible) without // Force a trip to the DC for evaluation, even if this could be // The call fails if the DC is not a GC // Enable cross forest trust referral } public enum DS_NAME_FORMAT // eg: CN=User Name,OU=Users,DC=Example,DC=Microsoft,DC=Com // eg: Example\\UserN // Probably"User Name" but could be something else. I.e. The // obsolete - see #define later // obsolete - see #define later // String-ized GUID as returned by IIDFromString(). // eg: example.microsoft.com/software/user name // eg: usern@example.microsoft.com // Same as DS_CANONICAL_NAME except that rightmost '/' is // eg: www/www.microsoft.com@example.com - generalized service principal // This is the string representation of a SID. Invalid for formatDesired. // Pseudo-name format so GetUserNameEx can return the DNS domain name to [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] [DllImport("ntdsapi.dll", CharSet = CharSet.Auto)] [DllImport("ntdsapi.dll", CharSet = CharSet.Auto)] [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] [STAThread] /// <summary> } |
相关讨论
- 谢谢你的努力。不幸的是,由于这涉及查询 AD,因此使用
System.DirectoryServices 命名空间似乎是一个更好的主意......虽然 - 这种方法可能会提供更好的性能。