diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2020-02-12 16:42:45 +0200 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2020-02-12 16:42:45 +0200 |
| commit | dd540257994b3af966109d6b57976786d3f23be8 (patch) | |
| tree | ba8726dadc25160811941cacf89ac62bef410172 /Software/Visual_Studio/Azure/Tango.AzureUtils | |
| parent | ffd8bbdb55ef8e99379dfb4aae2a48eca72f70ff (diff) | |
| download | Tango-dd540257994b3af966109d6b57976786d3f23be8.tar.gz Tango-dd540257994b3af966109d6b57976786d3f23be8.zip | |
Added some azure utils AD methods.
Diffstat (limited to 'Software/Visual_Studio/Azure/Tango.AzureUtils')
| -rw-r--r-- | Software/Visual_Studio/Azure/Tango.AzureUtils/ActiveDirectory/ActiveDirectoryManager.cs | 34 | ||||
| -rw-r--r-- | Software/Visual_Studio/Azure/Tango.AzureUtils/AzureUtilsAuthenticationFactory.cs | 17 |
2 files changed, 45 insertions, 6 deletions
diff --git a/Software/Visual_Studio/Azure/Tango.AzureUtils/ActiveDirectory/ActiveDirectoryManager.cs b/Software/Visual_Studio/Azure/Tango.AzureUtils/ActiveDirectory/ActiveDirectoryManager.cs index d6e1df042..26ce44b90 100644 --- a/Software/Visual_Studio/Azure/Tango.AzureUtils/ActiveDirectory/ActiveDirectoryManager.cs +++ b/Software/Visual_Studio/Azure/Tango.AzureUtils/ActiveDirectory/ActiveDirectoryManager.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Azure.ActiveDirectory.GraphClient; +using Microsoft.Azure.ActiveDirectory.GraphClient.Extensions; using Microsoft.Azure.Management.Fluent; using Microsoft.IdentityModel.Clients.ActiveDirectory; @@ -183,6 +184,39 @@ namespace Tango.AzureUtils.ActiveDirectory return users; } + public List<Group> GetUserGroups(String email) + { + var client = GetActiveDirectoryClient(); + + var user = client.Users.Where(x => x.UserPrincipalName == email).ExecuteSingleAsync().Result; + + var userFetcher = (IUserFetcher)user; + + List<Group> groups = new List<Group>(); + + IPagedCollection<IDirectoryObject> pagedCollection = userFetcher.MemberOf.ExecuteAsync().Result; + do + { + List<IDirectoryObject> directoryObjects = pagedCollection.CurrentPage.ToList(); + foreach (IDirectoryObject directoryObject in directoryObjects) + { + if (directoryObject is Group) + { + var group = directoryObject as Group; + groups.Add(group); + } + } + pagedCollection = pagedCollection.GetNextPageAsync().Result; + } while (pagedCollection != null); + + return groups; + } + + public bool IsUserMemberOf(String group, String email) + { + return GetUserGroups(email).Exists(x => x.DisplayName == group); + } + #endregion } } diff --git a/Software/Visual_Studio/Azure/Tango.AzureUtils/AzureUtilsAuthenticationFactory.cs b/Software/Visual_Studio/Azure/Tango.AzureUtils/AzureUtilsAuthenticationFactory.cs index 53a725047..0d8b2ccfc 100644 --- a/Software/Visual_Studio/Azure/Tango.AzureUtils/AzureUtilsAuthenticationFactory.cs +++ b/Software/Visual_Studio/Azure/Tango.AzureUtils/AzureUtilsAuthenticationFactory.cs @@ -11,7 +11,7 @@ namespace Tango.AzureUtils public class AzureUtilsAuthenticationFactory { private static IAzure _azure; - private static AzureUtilsCredentials credentials = new AzureUtilsCredentials() + private static AzureUtilsCredentials _credentials = new AzureUtilsCredentials() { ClientID = "be33437c-5052-449f-ab9d-a88d008eae24", ClientSecret = "bf67fb6f-4d06-4893-988c-6b347aff23d6", @@ -19,6 +19,11 @@ namespace Tango.AzureUtils SubscriptionID = "10c8aa60-3b15-4e0d-b412-6aeef90e5e91" }; + public static void SetCredentials(AzureUtilsCredentials credentials) + { + _credentials = credentials; + } + public static Task<IAzure> AuthenticateOrGetAsync() { if (_azure == null) @@ -26,12 +31,12 @@ namespace Tango.AzureUtils return Task.Factory.StartNew<IAzure>(() => { var creds = SdkContext.AzureCredentialsFactory.FromServicePrincipal( - credentials.ClientID, - credentials.ClientSecret, - credentials.TenantID, + _credentials.ClientID, + _credentials.ClientSecret, + _credentials.TenantID, AzureEnvironment.AzureGlobalCloud); - _azure = Azure.Authenticate(creds).WithSubscription(credentials.SubscriptionID); + _azure = Azure.Authenticate(creds).WithSubscription(_credentials.SubscriptionID); return _azure; }); } @@ -43,7 +48,7 @@ namespace Tango.AzureUtils public static AzureUtilsCredentials GetCredentials() { - return credentials; + return _credentials; } } } |
