aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Synchronization/packages.config
blob: 4d5b3dd7488cd48d1f128a9ad38c65da49025873 (plain)
1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="EntityFramework" version="6.2.0" targetFramework="net461" />
  <package id="System.Data.SQLite" version="1.0.108.0" targetFramework="net46" />
  <package id="System.Data.SQLite.Core" version="1.0.108.0" targetFramework="net46" />
  <package id="System.Data.SQLite.EF6" version="1.0.108.0" targetFramework="net46" />
  <package id="System.Data.SQLite.Linq" version="1.0.108.0" targetFramework="net46" />
</packages>
se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
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);
            authContext.TokenCache.Clear();
            UserCredential userCredential = new UserPasswordCredential(email, password);
            AuthenticationResult authResult = authContext.AcquireTokenAsync("https://graph.windows.net/", WebConfig.CLIENT_ID, userCredential).Result;
            return authResult;
        }

        private AuthenticationResult GetAppAuthenticationResult()
        {
            var authContext = new AuthenticationContext(_service_root);
            ClientCredential clientCredentials = new ClientCredential(WebConfig.CLIENT_ID, WebConfig.APP_SECRET);
            AuthenticationResult authResult = authContext.AcquireTokenAsync("https://graph.windows.net/", clientCredentials).Result;
            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);
        }
    }
}