|
24 | 24 | using System.Collections;
|
25 | 25 | using System.Collections.Generic;
|
26 | 26 | using System.Linq;
|
| 27 | +using System.Linq.Expressions; |
27 | 28 | using System.Threading.Tasks;
|
28 | 29 | using PSKeyVaultModels = Microsoft.Azure.Commands.KeyVault.Models;
|
29 | 30 | using PSKeyVaultProperties = Microsoft.Azure.Commands.KeyVault.Properties;
|
@@ -193,17 +194,15 @@ protected string GetObjectId(string objectId, string upn, string spn)
|
193 | 194 | if (!string.IsNullOrWhiteSpace(upn))
|
194 | 195 | {
|
195 | 196 | objectFilter = upn;
|
196 |
| - var user = ActiveDirectoryClient.Users.Where(u => |
197 |
| - u.UserPrincipalName.Equals(upn) || u.Mail.Equals(upn) || u.OtherMails.Any(m => m.Equals(upn))). |
198 |
| - ExecuteAsync().GetAwaiter().GetResult().CurrentPage.FirstOrDefault(); |
| 197 | + var user = ActiveDirectoryClient.Users.Where(FilterByUpn(upn)).ExecuteAsync().GetAwaiter().GetResult().CurrentPage.FirstOrDefault(); |
199 | 198 | if (user != null)
|
200 | 199 | objId = user.ObjectId;
|
201 | 200 | }
|
202 | 201 | else if (!string.IsNullOrWhiteSpace(spn))
|
203 | 202 | {
|
204 | 203 | objectFilter = spn;
|
205 | 204 | var servicePrincipal = ActiveDirectoryClient.ServicePrincipals.Where(s =>
|
206 |
| - s.ServicePrincipalNames.Any(n => n.Equals(spn))) |
| 205 | + s.ServicePrincipalNames.Any(n => n.Equals(spn, StringComparison.OrdinalIgnoreCase))) |
207 | 206 | .ExecuteAsync().GetAwaiter().GetResult().CurrentPage.FirstOrDefault();
|
208 | 207 | if (servicePrincipal != null)
|
209 | 208 | objId = servicePrincipal.ObjectId;
|
@@ -246,6 +245,19 @@ protected bool IsValidObjectIdSyntax(string objectId)
|
246 | 245 | return Guid.TryParse(objectId, out dummyValue);
|
247 | 246 | }
|
248 | 247 |
|
| 248 | + private Expression<Func<IUser, bool>> FilterByUpn(string upn) |
| 249 | + { |
| 250 | + // In ADFS, Graph cannot handle this particular combination of filters. |
| 251 | + if (!DefaultProfile.Context.Environment.OnPremise) |
| 252 | + { |
| 253 | + return u => u.UserPrincipalName.Equals(upn, StringComparison.OrdinalIgnoreCase) || |
| 254 | + u.Mail.Equals(upn, StringComparison.OrdinalIgnoreCase) || |
| 255 | + u.OtherMails.Any(m => m.Equals(upn, StringComparison.OrdinalIgnoreCase)); |
| 256 | + } |
| 257 | + |
| 258 | + return u => u.UserPrincipalName.Equals(upn, StringComparison.OrdinalIgnoreCase); |
| 259 | + } |
| 260 | + |
249 | 261 | protected readonly string[] DefaultPermissionsToKeys =
|
250 | 262 | {
|
251 | 263 | "get",
|
|
0 commit comments