How to create WindowsIdentity/WindowsPrincipal from username in DOMAIN\user format
WindowsIdentity(string) 构造函数要求用户名为 username@domain.com 格式。但在我的情况下,我从数据库中以旧的 DOMAIN\\user 格式获取用户名(然后必须检查他们的 Windows 角色成员身份)。
从旧样式 (sAMAccountName) 用户名??创建 WindowsPrincipal 的最佳方法是什么?
- 您可以使用 DsCrackNames API 来转换格式。
- 我在这里找到了一个 DsCrackNames C# 示例:technolog.nl/blogs/eprogrammer/archive/2005/11/16/…2800_upda??te_2900.aspx
似乎没有办法在不涉及对 Active Directory 的查询的情况下转换用户名格式。由于是这种情况,因此无需创建 WindowsPrincipal 来检查组成员身份,因为这可能需要另一个与 AD 的连接。
通过使用 System.DirectoryServices.AccountManagement 命名空间,您可以获得用户的 UPN 并检查组成员。
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
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 存储的查询(取决于上下文)…
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
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 标志,可用于不点击目录,但我认为这不会在这里工作。
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
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 命名空间似乎是一个更好的主意……虽然 – 这种方法可能会提供更好的性能。
来源:https://www.codenong.com/18835134/