{"id":1394,"date":"2016-04-13T17:40:37","date_gmt":"2016-04-13T15:40:37","guid":{"rendered":"http:\/\/hen.co.za\/blog\/?p=1394"},"modified":"2016-04-13T17:40:37","modified_gmt":"2016-04-13T15:40:37","slug":"hiding-passwords-in-c-app-console","status":"publish","type":"post","link":"https:\/\/hen.co.za\/blog\/2016\/04\/hiding-passwords-in-c-app-console\/","title":{"rendered":"Hiding passwords in (C# App) console"},"content":{"rendered":"<p>I&#8217;m actually a bit surprised that despite .Net\/C# being more than 10 years old there is still no native Console.ReadLine() overload that hides the characters you are tying &#8211; for when you want to prompt for a password or so. Of course writing your own version to do this is not really difficult but still it would have been nice&#8230;<\/p>\n<p>Here is one quick version&#8230; in case someone else wants to have one (and is too lazy to create it from scratch) hehe.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n        private static string GetPwd()\r\n        {\r\n            string pass = &quot;&quot;;\r\n            Console.Write(&quot;Enter your password: &quot;);\r\n            ConsoleKeyInfo key;\r\n\r\n            do\r\n            {\r\n                key = Console.ReadKey(true);\r\n\r\n                \/\/ Backspace Should Not Work\r\n                if (key.Key != ConsoleKey.Backspace &amp;&amp; key.Key != ConsoleKey.Enter)\r\n                {\r\n                    pass += key.KeyChar;\r\n                    Console.Write(&quot;*&quot;);\r\n                }\r\n                else if (key.Key == ConsoleKey.Backspace) \r\n                {\r\n                    if (pass.Length &gt; 0)\r\n                        pass = pass.Substring(0, pass.Length - 1);\r\n                    Console.Write(&quot;\\b \\b&quot;);\r\n                }\r\n            }            \r\n            \/\/ Stops Receving Keys Once Enter is Pressed\r\n            while (key.Key != ConsoleKey.Enter); \r\n            Console.WriteLine();\r\n            return pass.TrimEnd('\\r', '\\n'); \r\n        }\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m actually a bit surprised that despite .Net\/C# being more than 10 years old there is still no native Console.ReadLine() overload that hides the characters you are tying &#8211; for when you want to prompt for a password or so. &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/hen.co.za\/blog\/2016\/04\/hiding-passwords-in-c-app-console\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,16],"tags":[7,56,307],"class_list":["post-1394","post","type-post","status-publish","format-standard","hentry","category-development","category-reference","tag-net","tag-console","tag-password"],"_links":{"self":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/1394","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/comments?post=1394"}],"version-history":[{"count":7,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/1394\/revisions"}],"predecessor-version":[{"id":1401,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/1394\/revisions\/1401"}],"wp:attachment":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/media?parent=1394"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/categories?post=1394"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/tags?post=1394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}