From 5bec920df45bb79e5912a97f2d0afc1a849adbd2 Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Sun, 23 Dec 2018 09:34:03 +0200 Subject: Fixed some issues with observables generator. --- .../Web/Tango.MachineService/Tango.MachineService.csproj | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'Software/Visual_Studio/Web/Tango.MachineService/Tango.MachineService.csproj') diff --git a/Software/Visual_Studio/Web/Tango.MachineService/Tango.MachineService.csproj b/Software/Visual_Studio/Web/Tango.MachineService/Tango.MachineService.csproj index c6245e18e..5b13d8afd 100644 --- a/Software/Visual_Studio/Web/Tango.MachineService/Tango.MachineService.csproj +++ b/Software/Visual_Studio/Web/Tango.MachineService/Tango.MachineService.csproj @@ -64,6 +64,12 @@ ..\..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.3\lib\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll + + ..\..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.2.7.10707.1513-rc\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.dll + + + ..\..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.2.7.10707.1513-rc\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms.dll + ..\..\packages\Microsoft.SqlServer.SqlManagementObjects.140.17283.0\lib\net40\Microsoft.SqlServer.AzureStorageEnum.dll -- cgit v1.3.1 From 4e496467dcc8549605917a44263d78cef40f71bb Mon Sep 17 00:00:00 2001 From: Roy Ben Shabat Date: Sun, 23 Dec 2018 12:44:08 +0200 Subject: Implemented machine studio active directory secure login. --- .../Build/Shortcuts/Machine Studio.lnk | Bin 1516 -> 1581 bytes .../MachineStudioSettings.cs | 5 +--- .../Tango.MachineStudio.Common.csproj | 4 +-- .../DefaultAuthenticationProvider.cs | 19 +++++++++++-- .../ViewModels/LoadingViewVM.cs | 5 ++-- .../ViewModels/LoginViewVM.cs | 16 +++++++---- .../Controllers/MachineStudioController.cs | 31 +++++++++++++-------- .../Controllers/PPCController.cs | 3 ++ .../Helpers/AzureDirectoryHelper.cs | 19 +++++++++++++ .../Tango.MachineService.csproj | 3 +- 10 files changed, 75 insertions(+), 30 deletions(-) create mode 100644 Software/Visual_Studio/Web/Tango.MachineService/Helpers/AzureDirectoryHelper.cs (limited to 'Software/Visual_Studio/Web/Tango.MachineService/Tango.MachineService.csproj') diff --git a/Software/Visual_Studio/Build/Shortcuts/Machine Studio.lnk b/Software/Visual_Studio/Build/Shortcuts/Machine Studio.lnk index adc17d481..9c594a6b0 100644 Binary files a/Software/Visual_Studio/Build/Shortcuts/Machine Studio.lnk and b/Software/Visual_Studio/Build/Shortcuts/Machine Studio.lnk differ diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/MachineStudioSettings.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/MachineStudioSettings.cs index cab3d2bfc..1d3660c5f 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/MachineStudioSettings.cs +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/MachineStudioSettings.cs @@ -39,10 +39,8 @@ namespace Tango.MachineStudio.Common public String LastVirtualMachineSerialNumber { get; set; } /// - /// Gets or sets the update service address. + /// Gets or sets the machine service address. /// - public String UpdateServiceAddress { get; set; } - public String MachineServiceAddress { get; set; } /// @@ -97,7 +95,6 @@ namespace Tango.MachineStudio.Common { LastBounds = new Rect(); LoggingCategories = new List(); - UpdateServiceAddress = "http://twine01/MachineStudioUpdateService/MachineStudioUpdateService.svc"; MachineServiceAddress = "http://machineservice.twine-srv.com/"; DefaultIssueReportTags = new List(); StudioModulesBounds = new List(); diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Tango.MachineStudio.Common.csproj b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Tango.MachineStudio.Common.csproj index ca4d95805..cd99b00d0 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Tango.MachineStudio.Common.csproj +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Tango.MachineStudio.Common.csproj @@ -127,7 +127,7 @@ - + @@ -310,7 +310,7 @@ - + \ No newline at end of file diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Authentication/DefaultAuthenticationProvider.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Authentication/DefaultAuthenticationProvider.cs index fd68ed8d1..3293dab3f 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Authentication/DefaultAuthenticationProvider.cs +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Authentication/DefaultAuthenticationProvider.cs @@ -10,6 +10,9 @@ using Tango.MachineStudio.Common.Authentication; using Tango.BL; using Tango.BL.Enumerations; using System.Data.Entity; +using Tango.Transport.Web; +using Tango.Settings; +using Tango.MachineStudio.Common; namespace Tango.MachineStudio.UI.Authentication { @@ -49,11 +52,21 @@ namespace Tango.MachineStudio.UI.Authentication /// Login failed for user " + email public User Login(string email, string password) { - using (ObservablesContext db = ObservablesContext.CreateDefault()) + var settings = SettingsManager.Default.GetOrCreate(); + + IWebTransportClient service = new WebTransportClient(); + var response = service.PostJson(settings.MachineServiceAddress + "/api/MachineStudio/Login", new LoginRequest() { - String hash = User.GetPasswordHash(password); + Email = email, + Password = password, + + }).Result; + ObservablesContext.OverrideSettingsDataSource(response.DataSource); + + using (ObservablesContext db = ObservablesContext.CreateDefault()) + { db.Roles.Load(); db.Permissions.Load(); db.RolesPermissions.Load(); @@ -62,7 +75,7 @@ namespace Tango.MachineStudio.UI.Authentication .Include(x => x.UsersRoles) .Include(x => x.Contact) .Include(x => x.Address) - .Include(x => x.Organization).SingleOrDefault(x => x.Email.ToLower() == email.ToLower() && x.Password == hash); + .Include(x => x.Organization).SingleOrDefault(x => x.Email.ToLower() == email.ToLower()); if (user == null) { diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/LoadingViewVM.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/LoadingViewVM.cs index 545d51ef2..6e8d0488b 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/LoadingViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/LoadingViewVM.cs @@ -144,9 +144,8 @@ namespace Tango.MachineStudio.UI.ViewModels Status = "Loading, please wait..."; - ObservablesStaticCollections.Instance.Initialize(); - - _eventLogger.Log(EventTypes.ApplicationStarted, "Application Started!"); + //ObservablesStaticCollections.Instance.Initialize(); + //_eventLogger.Log(EventTypes.ApplicationStarted, "Application Started!"); Status = "Starting application..."; diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/LoginViewVM.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/LoginViewVM.cs index a37879a5e..4e659d46a 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/LoginViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/LoginViewVM.cs @@ -6,6 +6,8 @@ using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; +using Tango.BL; +using Tango.BL.Enumerations; using Tango.Core.Commands; using Tango.Core.Cryptography; using Tango.MachineStudio.Common; @@ -90,7 +92,7 @@ namespace Tango.MachineStudio.UI.ViewModels _navigationManager = navigationManager; _authenticationProvider = authenticationProvider; _eventLogger = eventLogger; - LoginCommand = new RelayCommand(Login,() => !IsLogging); + LoginCommand = new RelayCommand(Login, () => !IsLogging); cryptographer = new Rfc2898Cryptographer(); Email = _settings.LastLoginEmail; @@ -121,6 +123,10 @@ namespace Tango.MachineStudio.UI.ViewModels await Task.Factory.StartNew(() => { _authenticationProvider.Login(Email, Password); + + ObservablesStaticCollections.Instance.Initialize(); + _eventLogger.Log(EventTypes.ApplicationStarted, "Application Started!"); + _navigationManager.NavigateTo(NavigationView.MainView); _settings.LastLoginEmail = Email; _settings.RememberMe = RememberMe; @@ -128,13 +134,13 @@ namespace Tango.MachineStudio.UI.ViewModels _settings.LastLoginPassword = RememberMe ? cryptographer.Encrypt(Password) : null; _settings.Save(); - _eventLogger.Log("User logged in"); + _eventLogger.Log("User logged in."); }); } - catch (Exception) + catch (Exception ex) { - - _notificationProvider.ShowError("Invalid credentials. Please try again."); + LogManager.Log(ex, "Login Error."); + _notificationProvider.ShowError($"The specified email or password was incorrect, or you don't have a permission to run this application.\nError: {ex.Message}"); } finally { diff --git a/Software/Visual_Studio/Web/Tango.MachineService/Controllers/MachineStudioController.cs b/Software/Visual_Studio/Web/Tango.MachineService/Controllers/MachineStudioController.cs index 8225d75e1..8485cd67d 100644 --- a/Software/Visual_Studio/Web/Tango.MachineService/Controllers/MachineStudioController.cs +++ b/Software/Visual_Studio/Web/Tango.MachineService/Controllers/MachineStudioController.cs @@ -14,6 +14,7 @@ using Tango.Core.Cryptography; using Tango.MachineService.Helpers; using Tango.MachineService.Models; using Tango.MachineStudio.Common.Authentication; +using System.Data.Entity; using Tango.MachineStudio.Common.Update; namespace Tango.MachineService.Controllers @@ -191,40 +192,45 @@ namespace Tango.MachineService.Controllers #endregion + [HttpPost] public LoginResponse Login(LoginRequest request) { - var authContext = new AuthenticationContext("https://login.microsoftonline.com/2ebd63a5-bc2f-41dc-9066-4409ed5e5dd4"); - UserCredential userCredential = new UserCredential(request.Email, request.Password); - AuthenticationResult authResult = authContext.AcquireToken("https://graph.windows.net/", "ec612854-7abc-457b-808a-5d0c5ba80c57", userCredential); + AuthenticationResult authResult = AzureDirectoryHelper.AuthenticateUser(request.Email, request.Password); using (ObservablesContext db = ObservablesContextHelper.CreateContext()) { - db.Roles.ToList(); db.Permissions.ToList(); db.UsersRoles.ToList(); db.RolesPermissions.ToList(); - var user = db.Users.SingleOrDefault(x => x.Email.ToLower() == request.Email.ToLower() && x.Password == request.Password); + var user = db.Users.SingleOrDefault(x => x.Email.ToLower() == request.Email.ToLower()); + + IHashGenerator g = new BasicHashGenerator(); if (user == null) { //Than add the user !! - - IHashGenerator g = new BasicHashGenerator(); - - BL.Entities.User new_user = new User(); + User new_user = new User(); new_user.Email = request.Email; - new_user.Password = g.Encrypt("Aa123456"); - new_user.Organization = db.Organizations.Single(x => x.Name == "Twine"); - new_user.Address = new Address() { }; + new_user.Password = g.Encrypt(request.Password); + new_user.Organization = db.Organizations.Include(x => x.Address).Single(x => x.Name == "Twine"); + new_user.Address = new_user.Organization.Address.Clone(); new_user.Contact = new Contact() { FirstName = authResult.UserInfo.GivenName, LastName = authResult.UserInfo.FamilyName, + FullName = authResult.UserInfo.GivenName + " " + authResult.UserInfo.FamilyName, + Email = request.Email, }; new_user.Roles.Add(db.Roles.Single(x => (Roles)x.Code == Roles.User)); } + else + { + user.Password = g.Encrypt(request.Password); + } + + db.SaveChanges(); } return new LoginResponse() @@ -234,6 +240,7 @@ namespace Tango.MachineService.Controllers Address = Config.DB_ADDRESS, Catalog = Config.DB_CATALOG, Type = Core.DataSourceType.Azure, + IntegratedSecurity = false, UserName = request.Email, Password = request.Password, } diff --git a/Software/Visual_Studio/Web/Tango.MachineService/Controllers/PPCController.cs b/Software/Visual_Studio/Web/Tango.MachineService/Controllers/PPCController.cs index 662883223..68f597fcc 100644 --- a/Software/Visual_Studio/Web/Tango.MachineService/Controllers/PPCController.cs +++ b/Software/Visual_Studio/Web/Tango.MachineService/Controllers/PPCController.cs @@ -108,6 +108,7 @@ namespace Tango.MachineService.Controllers Catalog = Config.DB_CATALOG, UserName = credentials.UserName, Password = credentials.Password, + IntegratedSecurity = false, Type = DataSourceType.SQLServer, }; @@ -173,6 +174,7 @@ namespace Tango.MachineService.Controllers Catalog = Config.DB_CATALOG, UserName = credentials.UserName, Password = credentials.Password, + IntegratedSecurity = false, Type = DataSourceType.SQLServer, }; } @@ -249,6 +251,7 @@ namespace Tango.MachineService.Controllers Catalog = Config.DB_CATALOG, UserName = credentials.UserName, Password = credentials.Password, + IntegratedSecurity = false, Type = DataSourceType.SQLServer, }; } diff --git a/Software/Visual_Studio/Web/Tango.MachineService/Helpers/AzureDirectoryHelper.cs b/Software/Visual_Studio/Web/Tango.MachineService/Helpers/AzureDirectoryHelper.cs new file mode 100644 index 000000000..fe7733323 --- /dev/null +++ b/Software/Visual_Studio/Web/Tango.MachineService/Helpers/AzureDirectoryHelper.cs @@ -0,0 +1,19 @@ +using Microsoft.IdentityModel.Clients.ActiveDirectory; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; + +namespace Tango.MachineService.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/Web/Tango.MachineService/Tango.MachineService.csproj b/Software/Visual_Studio/Web/Tango.MachineService/Tango.MachineService.csproj index 5b13d8afd..c05cb5fe7 100644 --- a/Software/Visual_Studio/Web/Tango.MachineService/Tango.MachineService.csproj +++ b/Software/Visual_Studio/Web/Tango.MachineService/Tango.MachineService.csproj @@ -277,6 +277,7 @@ + @@ -379,7 +380,7 @@ False - + -- cgit v1.3.1