Tell me how to link the password protection for the arguments? The arguments need to be protected by a password, let's say we enter C:\>app.exe password /off and the C:\>app.exe password /off is first sent, if it is correct, the /off command is executed (all in one line).

Maybe there is an opportunity to somehow embed a password to the name of the arguments? Since it is necessary for the application shortcut to contain both a password and arguments for using the task scheduler.

The password itself will be stored in a text document, see the code example below.

Clarification of the question: how to insert a separate code for the password into the main code?

The main code of the WinForm C # application

 static class Program { [DllImport("kernel32.dll")] private static extern bool AttachConsole(int procid); /// <summary> /// Главная точка входа для приложения. /// </summary> [STAThread] static void Main(string[] args) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); var form = new Form1(); bool needRun = true; AttachConsole(-1); string outputFmt = "Команда {0} выполнена" + "\n" + "Для продолжения нажмите любую клавишу . . ."; List<string> cmds = new List<string>(); foreach (string arg in args) { if (args.Contains("/?")) { form.Check00(); needRun = false; } switch (arg) { case "/on": form.Check01(); needRun = false; break; case "/off": form.Check02(); needRun = false; break; } cmds.Add(arg); } Console.WriteLine(outputFmt, string.Join(" ", cmds.ToArray())); if (needRun) { Application.Run(form); } } } 

Separate password protection code (which cannot be linked):

  private class ConsolePassword { public ConsolePassword() { string password, password1 = string.Empty; password = Console.ReadLine(); using (StreamReader sr = new StreamReader(File.Open("C:\\1.txt", FileMode.Open))) { password1 = sr.ReadLine(); sr.Close(); } if (password == password1) { Console.WriteLine("Доступ разрешен."); } else { Console.WriteLine("Доступ запрещен."); } } } 
  • I think you should redo the ConsolePassword method ConsolePassword that it ConsolePassword entered password as an argument and checks for its correctness. It seems to me that it would be better to call the password1 variable real_password . It's not entirely clear what the question is - mymedia
  • @mymedia you need to insert a separate code for the password into the main code, this is the whole question of how to do it :) I cannot bind them ... - Vitokhv
  • You need to reformulate or change the question. You want to get an answer to this: "How to protect the execution of arguments with a password?" or to this: "Clarifying the question: how to insert a separate code for a password into the main code?"? - Bulson
  • @Bulson essentially does not change, the problem is that the arguments are password protected. - Vitokhv
  • This does not change the essence for you. And for the site where you ask the question it is important, because a knowledge base is being formed, and therefore be kind enough to formulate your question in a uniquely understandable way. - Bulson

1 answer 1

Your idea, how to say it, is a bit strange (think, maybe you should at least encrypt the password in a file, and not store it in clear form). But if you really want, then you can use this option. Class for reading the password from the file and checking the password

 public static class ConsolePassword { //Fields private const string _PasswordFile = @"C:\1.txt"; /// <summary> /// Проверка пароля /// </summary> /// <param name="password">строка пароля для проверки</param> /// <returns>true if OK</returns> public static bool CheckPassword(string password) { //проверка параметра if (String.IsNullOrEmpty(password)) throw new ArgumentException(nameof(password)); //считываем пароль для сравнения из файла string origPassword = GetPasswordFromFile(); if (String.IsNullOrEmpty(origPassword)) return false; //сравниваем и отдаем результат return origPassword.Equals(password); } private static string GetPasswordFromFile() { try { //если у вас в этом файле всего одна строка с паролем //то проще всего прочитать так string result = File.ReadAllText(_PasswordFile); return result; } catch (Exception ex) { Console.WriteLine($"Ошибка чтения файла {ex.Message}"); return String.Empty; } } } 

Then you can use this class as

 class Program { static void Main(string[] args) { //как-то вы там получаете от пользователя пароль string password = "???"; //проверяете и делаете остальное if (ConsolePassword.CheckPassword(password)) { //пароль прошел проверку } else { //пароль не прошел проверку Console.WriteLine("Пароль не верен!"); } } } 
  • string password = "???"; How can you do without this method? For a user to change a password in a text document, or will each user have to invent their own password inside the application? Or is there a way to change this property through the form? - Vitokhv
  • Bulson and it turns out that the password for the arguments do not need to enter, it is inside the application and is compared with the contents of the text file. - Vitokhv
  • By the way, it would make sense to take part of the password from the application and part from the text file. And so, you can even check the password in the registry. - Vitokhv
  • one
    @Vitokhv I tried to answer the question: "how to insert a separate code for the password into the main code?" By rewriting your ConsolePassword class with the following example of how it can be used. It's all. Your idea of ​​launching a program with checking a password from a text file is, in principle, I don’t like because of its initial crap. But if you still want to bring the matter to the end, consider yourself further the algorithm for obtaining a password, and so on. Ask questions (not in the comments to this answer), who will be able to help you. - Bulson