diff --git a/Samples/GDS/ConsoleServer/Program.cs b/Samples/GDS/ConsoleServer/Program.cs index 3950a97f1..8599b8199 100644 --- a/Samples/GDS/ConsoleServer/Program.cs +++ b/Samples/GDS/ConsoleServer/Program.cs @@ -29,6 +29,7 @@ using Mono.Options; using Opc.Ua.Configuration; +using Opc.Ua.Gds.Server.Database; using Opc.Ua.Gds.Server.Database.Linq; using Opc.Ua.Server; using System; @@ -243,33 +244,9 @@ private async Task ConsoleGlobalDiscoveryServer() var database = JsonApplicationsDatabase.Load(databaseStorePath); var userDatabase = JsonUsersDatabase.Load(userdatabaseStorePath); - - //configure Users - ApplicationInstance.MessageDlg.Message("Use default users?", true); - bool createStandardUsers = ApplicationInstance.MessageDlg.ShowAsync().Result; - - if (!createStandardUsers){ - //delete existing standard users - userDatabase.DeleteUser("appadmin"); - userDatabase.DeleteUser("appuser"); - userDatabase.DeleteUser("sysadmin"); - - //Create new admin user - Console.Write("Please specify user name of the application admin user:"); - string username = Console.ReadLine(); - _ = username ?? throw new ArgumentNullException("User name is not allowed to be empty"); - - Console.Write($"Please specify the password of {username}:"); - - //string password = Console.ReadLine(); - string password = GetPassword(); - _ = password ?? throw new ArgumentNullException("Password is not allowed to be empty"); - - userDatabase.CreateUser(username, password, GdsRole.ApplicationAdmin); - } + bool createStandardUsers = ConfigureUsers(userDatabase); // start the server. - server = new GlobalDiscoverySampleServer( database, database, @@ -296,6 +273,39 @@ private async Task ConsoleGlobalDiscoveryServer() } + private bool ConfigureUsers(JsonUsersDatabase userDatabase) + { + ApplicationInstance.MessageDlg.Message("Use default users?", true); + bool createStandardUsers = ApplicationInstance.MessageDlg.ShowAsync().Result; + + if (!createStandardUsers) + { + //delete existing standard users + userDatabase.DeleteUser("appadmin"); + userDatabase.DeleteUser("appuser"); + userDatabase.DeleteUser("sysadmin"); + + //Create new admin user + Console.Write("Please specify user name of the application admin user:"); + string username = Console.ReadLine(); + _ = username ?? throw new ArgumentNullException("User name is not allowed to be empty"); + + Console.Write($"Please specify the password of {username}:"); + + //string password = Console.ReadLine(); + string password = GetPassword(); + _ = password ?? throw new ArgumentNullException("Password is not allowed to be empty"); + + //create User, if User exists delete & recreate + if (!userDatabase.CreateUser(username, password, GdsRole.ApplicationAdmin)) + { + userDatabase.DeleteUser(username); + userDatabase.CreateUser(username, password, GdsRole.ApplicationAdmin); + } + } + return createStandardUsers; + } + private void EventStatus(Session session, SessionEventReason reason) { lastEventTime = DateTime.UtcNow; diff --git a/Samples/GDS/Server/Program.cs b/Samples/GDS/Server/Program.cs index afd107918..6a1ef3b54 100644 --- a/Samples/GDS/Server/Program.cs +++ b/Samples/GDS/Server/Program.cs @@ -74,27 +74,8 @@ static void Main() //initialize users Database userDatabase.Initialize(); - //configure users - ApplicationInstance.MessageDlg.Message("Use default users?", true); - bool createStandardUsers = ApplicationInstance.MessageDlg.ShowAsync().Result; - if (!createStandardUsers) - { - //Delete existing standard users - userDatabase.DeleteUser("appadmin"); - userDatabase.DeleteUser("appuser"); - userDatabase.DeleteUser("sysadmin"); - - //Create new admin user - string username = InputDlg.Show("Please specify user name of the application admin user:", false); - _ = username ?? throw new ArgumentNullException("User name is not allowed to be empty"); - - Console.Write($"Please specify the password of {username}:"); - - string password = InputDlg.Show($"Please specify the password of {username}:", true); - _ = password ?? throw new ArgumentNullException("Password is not allowed to be empty"); - - userDatabase.CreateUser(username, password, GdsRole.ApplicationAdmin); - } + bool createStandardUsers = ConfigureUsers(userDatabase); + // start the server. var database = new SqlApplicationsDatabase(); @@ -115,5 +96,35 @@ static void Main() ExceptionDlg.Show(application.ApplicationName, e); } } + + private static bool ConfigureUsers(SqlUsersDatabase userDatabase) + { + ApplicationInstance.MessageDlg.Message("Use default users?", true); + bool createStandardUsers = ApplicationInstance.MessageDlg.ShowAsync().Result; + if (!createStandardUsers) + { + //Delete existing standard users + userDatabase.DeleteUser("appadmin"); + userDatabase.DeleteUser("appuser"); + userDatabase.DeleteUser("sysadmin"); + + //Create new admin user + string username = InputDlg.Show("Please specify user name of the application admin user:", false); + _ = username ?? throw new ArgumentNullException("User name is not allowed to be empty"); + + Console.Write($"Please specify the password of {username}:"); + + string password = InputDlg.Show($"Please specify the password of {username}:", true); + _ = password ?? throw new ArgumentNullException("Password is not allowed to be empty"); + + //create User, if User exists delete & recreate + if (!userDatabase.CreateUser(username, password, GdsRole.ApplicationAdmin)) + { + userDatabase.DeleteUser(username); + userDatabase.CreateUser(username, password, GdsRole.ApplicationAdmin); + } + } + return createStandardUsers; + } } }