diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-12-27 15:14:10 +0200 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-12-27 15:14:10 +0200 |
| commit | 9e42e1c87f3a206f0babc74760ac9a02d8d328f4 (patch) | |
| tree | be3be4cf23f524f430146af472883f63dd8bdfb7 /Software/Visual_Studio/Tango.Web/ActiveDirectory | |
| parent | 894d05d59c0e1612903f1adbf908914f2df67ccc (diff) | |
| download | Tango-9e42e1c87f3a206f0babc74760ac9a02d8d328f4.tar.gz Tango-9e42e1c87f3a206f0babc74760ac9a02d8d328f4.zip | |
Implemented Deployment Slots!
Implemented Environment AD Groups.
Implemented Machine Studio environment selection.
Diffstat (limited to 'Software/Visual_Studio/Tango.Web/ActiveDirectory')
| -rw-r--r-- | Software/Visual_Studio/Tango.Web/ActiveDirectory/ActiveDirectoryManager.cs | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/Software/Visual_Studio/Tango.Web/ActiveDirectory/ActiveDirectoryManager.cs b/Software/Visual_Studio/Tango.Web/ActiveDirectory/ActiveDirectoryManager.cs new file mode 100644 index 000000000..d2eeb15a5 --- /dev/null +++ b/Software/Visual_Studio/Tango.Web/ActiveDirectory/ActiveDirectoryManager.cs @@ -0,0 +1,66 @@ +using Microsoft.Azure.ActiveDirectory.GraphClient; +using Microsoft.Azure.ActiveDirectory.GraphClient.Extensions; +using Microsoft.IdentityModel.Clients.ActiveDirectory; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tango.Web.ActiveDirectory +{ + public class ActiveDirectoryManager + { + private String _service_root = $"https://login.microsoftonline.com/{WebConfig.TENANT_ID}"; + + public AuthenticationResult ValidateUserCredentials(String email, String password) + { + var authContext = new AuthenticationContext(_service_root); + UserCredential userCredential = new UserCredential(email, password); + AuthenticationResult authResult = authContext.AcquireToken("https://graph.windows.net/", WebConfig.CLIENT_ID, userCredential); + return authResult; + } + + private AuthenticationResult GetAppAuthenticationResult() + { + var authContext = new AuthenticationContext(_service_root); + ClientCredential clientCredentials = new ClientCredential(WebConfig.CLIENT_ID, WebConfig.APP_SECRET); + AuthenticationResult authResult = authContext.AcquireToken("https://graph.windows.net/", clientCredentials); + return authResult; + } + + public List<Group> GetUserGroups(String email) + { + var authResult = GetAppAuthenticationResult(); + ActiveDirectoryClient activeDirectoryClient = new ActiveDirectoryClient(new Uri($"https://graph.windows.net/{WebConfig.TENANT_ID}"), async () => await Task.FromResult(authResult.AccessToken)); + var user = activeDirectoryClient.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 CanUserAccessCurrentEnvironment(String email) + { + var groups = GetUserGroups(email); + return groups.Exists(x => x.DisplayName == WebConfig.ENVIRONMENT_GROUP); + } + } +} |
