12
12
// limitations under the License.
13
13
// ----------------------------------------------------------------------------------
14
14
15
+ #if NETSTANDARD
15
16
using Microsoft . Azure . Graph . RBAC . Version1_6 . ActiveDirectory ;
17
+ #else
18
+ using Microsoft . Azure . ActiveDirectory . GraphClient ;
19
+ #endif
16
20
using Microsoft . Azure . Commands . Common . Authentication ;
17
21
using Microsoft . Azure . Commands . Common . Authentication . Abstractions ;
18
22
using Microsoft . Azure . Commands . Common . Authentication . Models ;
@@ -67,7 +71,13 @@ public ActiveDirectoryClient ActiveDirectoryClient
67
71
if ( _activeDirectoryClient == null )
68
72
{
69
73
_dataServiceCredential = new DataServiceCredential ( AzureSession . Instance . AuthenticationFactory , DefaultProfile . DefaultContext , AzureEnvironment . Endpoint . Graph ) ;
74
+ #if NETSTANDARD
70
75
_activeDirectoryClient = new ActiveDirectoryClient ( DefaultProfile . DefaultContext ) ;
76
+ #else
77
+ _activeDirectoryClient = new ActiveDirectoryClient ( new Uri ( string . Format ( "{0}/{1}" ,
78
+ DefaultProfile . DefaultContext . Environment . GetEndpoint ( AzureEnvironment . Endpoint . Graph ) , _dataServiceCredential . TenantId ) ) ,
79
+ ( ) => Task . FromResult ( _dataServiceCredential . GetToken ( ) ) ) ;
80
+ #endif
71
81
}
72
82
return this . _activeDirectoryClient ;
73
83
}
@@ -204,7 +214,13 @@ protected string GetCurrentUsersObjectId()
204
214
string objectId = null ;
205
215
if ( DefaultContext . Account . Type == AzureAccount . AccountType . User )
206
216
{
207
- objectId = ActiveDirectoryClient . GetObjectId ( new ADObjectFilterOptions ( ) ) . ToString ( ) ;
217
+ #if NETSTANDARD
218
+ objectId = ActiveDirectoryClient . GetObjectId ( new ADObjectFilterOptions { UPN = DefaultContext . Account . Id } ) . ToString ( ) ;
219
+ #else
220
+ var userFetcher = ActiveDirectoryClient . Me . ToUser ( ) ;
221
+ var user = userFetcher . ExecuteAsync ( ) . Result ;
222
+ objectId = user . ObjectId ;
223
+ #endif
208
224
}
209
225
210
226
return objectId ;
@@ -225,9 +241,20 @@ private string GetObjectIdByUpn(string upn)
225
241
string objId = null ;
226
242
if ( ! string . IsNullOrWhiteSpace ( upn ) )
227
243
{
228
- var user = ActiveDirectoryClient . FilterUsers ( new ADObjectFilterOptions ( ) { SPN = upn } ) . SingleOrDefault ( ) ;
244
+ #if NETSTANDARD
245
+ var user = ActiveDirectoryClient . FilterUsers ( new ADObjectFilterOptions ( ) { UPN = upn } ) . SingleOrDefault ( ) ;
246
+ #else
247
+ var user = ActiveDirectoryClient . Users . Where ( u => u . UserPrincipalName . Equals ( upn , StringComparison . OrdinalIgnoreCase ) )
248
+ . ExecuteAsync ( ) . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) . CurrentPage . SingleOrDefault ( ) ;
249
+ #endif
229
250
if ( user != null )
251
+ {
252
+ #if NETSTANDARD
230
253
objId = user . Id . ToString ( ) ;
254
+ #else
255
+ objId = user . ObjectId ;
256
+ #endif
257
+ }
231
258
}
232
259
return objId ;
233
260
}
@@ -237,9 +264,15 @@ private string GetObjectIdBySpn(string spn)
237
264
string objId = null ;
238
265
if ( ! string . IsNullOrWhiteSpace ( spn ) )
239
266
{
267
+ #if NETSTANDARD
240
268
var servicePrincipal = ActiveDirectoryClient . FilterServicePrincipals ( new ADObjectFilterOptions ( ) { SPN = spn } ) . SingleOrDefault ( ) ;
241
- if ( servicePrincipal != null )
242
- objId = servicePrincipal . Id . ToString ( ) ;
269
+ objId = servicePrincipal ? . Id . ToString ( ) ;
270
+ #else
271
+ var servicePrincipal = ActiveDirectoryClient . ServicePrincipals . Where ( s =>
272
+ s . ServicePrincipalNames . Any ( n => n . Equals ( spn , StringComparison . OrdinalIgnoreCase ) ) )
273
+ . ExecuteAsync ( ) . GetAwaiter ( ) . GetResult ( ) . CurrentPage . SingleOrDefault ( ) ;
274
+ objId = servicePrincipal ? . ObjectId ;
275
+ #endif
243
276
}
244
277
return objId ;
245
278
}
@@ -250,23 +283,44 @@ private string GetObjectIdByEmail(string email)
250
283
// In ADFS, Graph cannot handle this particular combination of filters.
251
284
if ( ! DefaultProfile . DefaultContext . Environment . OnPremise && ! string . IsNullOrWhiteSpace ( email ) )
252
285
{
286
+ #if NETSTANDARD
253
287
var users = ActiveDirectoryClient . FilterUsers ( new ADObjectFilterOptions ( ) { Mail = email } ) ;
254
288
if ( users != null )
255
289
{
256
290
ThrowIfMultipleObjectIds ( users , email ) ;
257
291
var user = users . FirstOrDefault ( ) ;
258
292
objId = user ? . Id . ToString ( ) ;
259
293
}
294
+ #else
295
+ var users = ActiveDirectoryClient . Users . Where ( FilterByEmail ( email ) ) . ExecuteAsync ( ) . GetAwaiter ( ) . GetResult ( ) . CurrentPage ;
296
+ if ( users != null )
297
+ {
298
+ ThrowIfMultipleObjectIds ( users , email ) ;
299
+ var user = users . FirstOrDefault ( ) ;
300
+ objId = user ? . ObjectId ;
301
+ }
302
+ #endif
260
303
}
261
304
return objId ;
262
305
}
263
306
307
+ #if ! NETSTANDARD
308
+ private Expression < Func < IUser , bool > > FilterByEmail ( string email )
309
+ {
310
+ return u => u . Mail . Equals ( email , StringComparison . OrdinalIgnoreCase ) ||
311
+ u . OtherMails . Any ( m => m . Equals ( email , StringComparison . OrdinalIgnoreCase ) ) ;
312
+ }
313
+ #endif
264
314
private bool ValidateObjectId ( string objId )
265
315
{
266
316
bool isValid = false ;
267
317
if ( ! string . IsNullOrWhiteSpace ( objId ) )
268
318
{
319
+ #if NETSTANDARD
269
320
var objectCollection = ActiveDirectoryClient . GetObjectsByObjectId ( new List < string > { objId } ) ;
321
+ #else
322
+ var objectCollection = ActiveDirectoryClient . GetObjectsByObjectIdsAsync ( new [ ] { objId } , new string [ ] { } ) . GetAwaiter ( ) . GetResult ( ) ;
323
+ #endif
270
324
if ( objectCollection . Any ( ) )
271
325
{
272
326
isValid = true ;
0 commit comments