public ActionResult FileShareDiagnostics()

in windows/aspnet-gmsa/dotnetdemoappMVC/Controllers/NetworkAccessController.cs [71:130]


        public ActionResult FileShareDiagnostics(string filePath)
        {
            var overallSuccess = true;
            var testResult = new StringBuilder();

            if (System.IO.File.Exists(filePath))
            {
                // Test read access
                try
                {
                    testResult.AppendLine($"[Info] Attempting to read from file '{filePath}'");
                    string fileContent = System.IO.File.ReadAllText(filePath);
                    testResult.AppendLine("[Pass] Successfully read file content. Content is:");
                    testResult.AppendLine(fileContent);
                }
                catch (Exception e)
                {
                    overallSuccess = false;
                    testResult.AppendLine("[Error] Could not read file content. Error is:");
                    testResult.AppendLine(e.Message);
                }
                // Test write access 
                try
                {
                    testResult.AppendLine($"[Info] Attempting to append to file '{filePath}'");
                    System.IO.File.AppendAllText(filePath, "Line added by web application", Encoding.UTF8);
                    overallSuccess = false;
                    testResult.AppendLine("[Error] Content was appended to the file. This is not the expected behavior. Please verify the gMSA has only read permissions over the file.");
                }
                catch (UnauthorizedAccessException)
                {
                    testResult.AppendLine("[Pass] gMSA not permitted to append content to the file. This is the expected behavior.");
                }

                catch (Exception e)
                {
                    overallSuccess = false;
                    testResult.AppendLine("[Error] Could not append content to the file. Error is:");
                    testResult.AppendLine(e.Message);
                }
            }
            else
            {
                overallSuccess = false;
                testResult.AppendLine($"[Error] Cloud not find file '{filePath}'. Either the path is incorrect, or the application is not configured to use the gMSA.");
            }

            testResult.AppendLine();

            if (overallSuccess)
            {
                testResult.AppendLine("[Pass] All tests were successful. The application is able to access network shares with the gMSA.");
            }
            else 
            {
                testResult.AppendLine("[Fail] Some or all tests failed. Refer to the error message(s) for additional information.");
            }

            return Content(testResult.ToString());
        }