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 | |
| 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')
7 files changed, 179 insertions, 21 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); + } + } +} diff --git a/Software/Visual_Studio/Tango.Web/DeploymentSlot.cs b/Software/Visual_Studio/Tango.Web/DeploymentSlot.cs new file mode 100644 index 000000000..676d0dc50 --- /dev/null +++ b/Software/Visual_Studio/Tango.Web/DeploymentSlot.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tango.Web +{ + public enum DeploymentSlot + { + [Description("Development")] + DEV, + [Description("Testing")] + TEST, + [Description("Staging")] + STAGE, + [Description("Production")] + PROD + } +} diff --git a/Software/Visual_Studio/Tango.Web/Helpers/AzureDirectoryHelper.cs b/Software/Visual_Studio/Tango.Web/Helpers/AzureDirectoryHelper.cs deleted file mode 100644 index 27b5e7cf5..000000000 --- a/Software/Visual_Studio/Tango.Web/Helpers/AzureDirectoryHelper.cs +++ /dev/null @@ -1,19 +0,0 @@ -using Microsoft.IdentityModel.Clients.ActiveDirectory; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Web; - -namespace Tango.Web.Helpers -{ - public static class AzureDirectoryHelper - { - public static AuthenticationResult AuthenticateUser(String email, String password) - { - var authContext = new AuthenticationContext("https://login.microsoftonline.com/2ebd63a5-bc2f-41dc-9066-4409ed5e5dd4"); - UserCredential userCredential = new UserCredential(email, password); - AuthenticationResult authResult = authContext.AcquireToken("https://graph.windows.net/", "ec612854-7abc-457b-808a-5d0c5ba80c57", userCredential); - return authResult; - } - } -}
\ No newline at end of file diff --git a/Software/Visual_Studio/Tango.Web/Tango.Web.csproj b/Software/Visual_Studio/Tango.Web/Tango.Web.csproj index 385b3bde8..70ac28e6e 100644 --- a/Software/Visual_Studio/Tango.Web/Tango.Web.csproj +++ b/Software/Visual_Studio/Tango.Web/Tango.Web.csproj @@ -69,11 +69,23 @@ <Reference Include="Microsoft.ApplicationInsights, Version=2.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> <HintPath>..\packages\Microsoft.ApplicationInsights.2.2.0\lib\net46\Microsoft.ApplicationInsights.dll</HintPath> </Reference> + <Reference Include="Microsoft.Azure.ActiveDirectory.GraphClient, Version=2.1.10.0, Culture=neutral, processorArchitecture=MSIL"> + <HintPath>..\packages\Microsoft.Azure.ActiveDirectory.GraphClient.2.1.1\lib\portable-net4+sl5+win+wpa+wp8\Microsoft.Azure.ActiveDirectory.GraphClient.dll</HintPath> + </Reference> <Reference Include="Microsoft.Azure.Common, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" /> <Reference Include="Microsoft.Azure.Common.NetFramework, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" /> <Reference Include="Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> <HintPath>..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.3\lib\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll</HintPath> </Reference> + <Reference Include="Microsoft.Data.Edm, Version=5.6.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> + <HintPath>..\packages\Microsoft.Data.Edm.5.6.4\lib\net40\Microsoft.Data.Edm.dll</HintPath> + </Reference> + <Reference Include="Microsoft.Data.OData, Version=5.6.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> + <HintPath>..\packages\Microsoft.Data.OData.5.6.4\lib\net40\Microsoft.Data.OData.dll</HintPath> + </Reference> + <Reference Include="Microsoft.Data.Services.Client, Version=5.6.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> + <HintPath>..\packages\Microsoft.Data.Services.Client.5.6.4\lib\net40\Microsoft.Data.Services.Client.dll</HintPath> + </Reference> <Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory, Version=2.7.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> <HintPath>..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.2.7.10707.1513-rc\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.dll</HintPath> </Reference> @@ -186,6 +198,9 @@ </Reference> <Reference Include="System.Runtime.Serialization" /> <Reference Include="System.Security" /> + <Reference Include="System.Spatial, Version=5.6.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> + <HintPath>..\packages\System.Spatial.5.6.4\lib\net40\System.Spatial.dll</HintPath> + </Reference> <Reference Include="System.Web" /> <Reference Include="System.Web.Helpers, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> <HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.Helpers.dll</HintPath> @@ -228,13 +243,14 @@ <Compile Include="..\Versioning\GlobalVersionInfo.cs"> <Link>GlobalVersionInfo.cs</Link> </Compile> + <Compile Include="ActiveDirectory\ActiveDirectoryManager.cs" /> <Compile Include="Authentication\TokensManager.cs" /> + <Compile Include="DeploymentSlot.cs" /> <Compile Include="Storage\ExtensionMethods.cs" /> <Compile Include="Storage\StorageManager.cs" /> <Compile Include="WebConfig.cs" /> <Compile Include="Controllers\JsonController.cs" /> <Compile Include="Formatters\JsonNetFormatter.cs" /> - <Compile Include="Helpers\AzureDirectoryHelper.cs" /> <Compile Include="Helpers\ObservablesContextHelper.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Formatters\ProtoBufFormatter.cs" /> @@ -243,6 +259,7 @@ <Compile Include="WebApiException.cs" /> </ItemGroup> <ItemGroup> + <None Include="app.config" /> <None Include="packages.config" /> </ItemGroup> <ItemGroup> diff --git a/Software/Visual_Studio/Tango.Web/WebConfig.cs b/Software/Visual_Studio/Tango.Web/WebConfig.cs index c970fa3a1..d9566a7c5 100644 --- a/Software/Visual_Studio/Tango.Web/WebConfig.cs +++ b/Software/Visual_Studio/Tango.Web/WebConfig.cs @@ -8,18 +8,55 @@ namespace Tango.Web { public class WebConfig { + /// <summary> + /// Gets the database address. + /// </summary> public static String DB_ADDRESS => ConfigurationManager.AppSettings[nameof(DB_ADDRESS)].ToString(); + + /// <summary> + /// Gets the name of the database user. + /// </summary> public static String DB_USER_NAME => ConfigurationManager.AppSettings[nameof(DB_USER_NAME)].ToString(); + + /// <summary> + /// Gets the database password. + /// </summary> public static String DB_PASSWORD => ConfigurationManager.AppSettings[nameof(DB_PASSWORD)].ToString(); + + /// <summary> + /// Gets the database catalog. + /// </summary> public static String DB_CATALOG => ConfigurationManager.AppSettings[nameof(DB_CATALOG)].ToString(); + /// <summary> + /// Gets the storage account URL. + /// </summary> public static String STORAGE_ACCOUNT => ConfigurationManager.AppSettings[nameof(STORAGE_ACCOUNT)].ToString(); + /// <summary> + /// Gets the tenant identifier. + /// </summary> public static String TENANT_ID => ConfigurationManager.AppSettings[nameof(TENANT_ID)].ToString(); + + /// <summary> + /// Gets the client identifier. + /// </summary> public static String CLIENT_ID => ConfigurationManager.AppSettings[nameof(CLIENT_ID)].ToString(); + + /// <summary> + /// Gets the application secret. + /// </summary> public static String APP_SECRET => ConfigurationManager.AppSettings[nameof(APP_SECRET)].ToString(); - public static String DEPLOYMENT_SLOT => ConfigurationManager.AppSettings[nameof(DEPLOYMENT_SLOT)].ToString(); + /// <summary> + /// Gets the deployment slot (DEV/TEST/STAGE/PROD). + /// </summary> + public static DeploymentSlot DEPLOYMENT_SLOT => (DeploymentSlot)Enum.Parse(typeof(DeploymentSlot), ConfigurationManager.AppSettings[nameof(DEPLOYMENT_SLOT)].ToString()); + + /// <summary> + /// Gets the environment active directory user group (Tango DEV / Tango TEST / Tango STAGE / Tango PROD). + /// </summary> + public static String ENVIRONMENT_GROUP => ConfigurationManager.AppSettings[nameof(ENVIRONMENT_GROUP)].ToString(); } } diff --git a/Software/Visual_Studio/Tango.Web/app.config b/Software/Visual_Studio/Tango.Web/app.config new file mode 100644 index 000000000..7c02d45bb --- /dev/null +++ b/Software/Visual_Studio/Tango.Web/app.config @@ -0,0 +1,31 @@ +<?xml version="1.0" encoding="utf-8"?> +<configuration> + <runtime> + <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> + <dependentAssembly> + <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" /> + <bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" /> + </dependentAssembly> + <dependentAssembly> + <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" culture="neutral" /> + <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" /> + </dependentAssembly> + <dependentAssembly> + <assemblyIdentity name="Microsoft.Data.Edm" publicKeyToken="31bf3856ad364e35" culture="neutral" /> + <bindingRedirect oldVersion="0.0.0.0-5.6.4.0" newVersion="5.6.4.0" /> + </dependentAssembly> + <dependentAssembly> + <assemblyIdentity name="Microsoft.Data.Services.Client" publicKeyToken="31bf3856ad364e35" culture="neutral" /> + <bindingRedirect oldVersion="0.0.0.0-5.6.4.0" newVersion="5.6.4.0" /> + </dependentAssembly> + <dependentAssembly> + <assemblyIdentity name="Microsoft.Data.OData" publicKeyToken="31bf3856ad364e35" culture="neutral" /> + <bindingRedirect oldVersion="0.0.0.0-5.6.4.0" newVersion="5.6.4.0" /> + </dependentAssembly> + <dependentAssembly> + <assemblyIdentity name="Microsoft.IdentityModel.Clients.ActiveDirectory" publicKeyToken="31bf3856ad364e35" culture="neutral" /> + <bindingRedirect oldVersion="0.0.0.0-2.7.0.0" newVersion="2.7.0.0" /> + </dependentAssembly> + </assemblyBinding> + </runtime> +</configuration>
\ No newline at end of file diff --git a/Software/Visual_Studio/Tango.Web/packages.config b/Software/Visual_Studio/Tango.Web/packages.config index 6266503ba..e7bb554d1 100644 --- a/Software/Visual_Studio/Tango.Web/packages.config +++ b/Software/Visual_Studio/Tango.Web/packages.config @@ -19,7 +19,11 @@ <package id="Microsoft.AspNet.WebApi.HelpPage" version="5.2.3" targetFramework="net461" /> <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net461" /> <package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net461" /> + <package id="Microsoft.Azure.ActiveDirectory.GraphClient" version="2.1.1" targetFramework="net461" /> <package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.3" targetFramework="net461" /> + <package id="Microsoft.Data.Edm" version="5.6.4" targetFramework="net461" /> + <package id="Microsoft.Data.OData" version="5.6.4" targetFramework="net461" /> + <package id="Microsoft.Data.Services.Client" version="5.6.4" targetFramework="net461" /> <package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="2.7.10707.1513-rc" targetFramework="net461" /> <package id="Microsoft.Net.Compilers" version="2.4.0" targetFramework="net461" developmentDependency="true" /> <package id="Microsoft.SqlServer.SqlManagementObjects" version="140.17283.0" targetFramework="net461" /> @@ -27,5 +31,6 @@ <package id="Modernizr" version="2.6.2" targetFramework="net461" /> <package id="Newtonsoft.Json" version="8.0.3" targetFramework="net461" /> <package id="Respond" version="1.2.0" targetFramework="net461" /> + <package id="System.Spatial" version="5.6.4" targetFramework="net461" /> <package id="WebGrease" version="1.5.2" targetFramework="net461" /> </packages>
\ No newline at end of file |
