2

The below program written in C# which runs fine in Windows but when it comes to running in Linux ( inside a docker container) it doesn't translate the path properly.

  class Program
    {
        static void Main(string[] args)
        {
            try {
                 bool validLogin = ValidateUser("domain",  "username", "password" );
                    if (validLogin)
                    {
                        var path = "\\\\10.123.123.123\\folder$\\subfolder";
                        string fullPath = Path.Combine("\\\\10.123.123.123\\folder$\\subfolder", "file_name1");
                        string body = "Test File Contents";
                        if (!Directory.Exists(path))
                        {
                            Directory.CreateDirectory((path));
                        }
                        File.WriteAllText(fullPath, body);   
                    }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString() + ex.Message);
            }
        }

        public static bool ValidateUser(string domainName, string username, string password)
        {
            string userDn = $"{username}@{domainName}";
            try
            {
                using (var connection = new LdapConnection {SecureSocketLayer = false})
                {
                    connection.Connect(domainName, LdapConnection.DefaultPort);
                    connection.Bind(userDn, password);
                    if (connection.Bound)
                        return true;
                }
            }
            catch (LdapException )
            {
                // Log exception
            }
            return false;
        }
    }

What exact path should I use? I have tried all sorts of combinations.

vkandvia
  • 85
  • 12

2 Answers2

1

In windows, you have "Local Paths" which start with a letter that refers to a local drive, then you have networked paths, which begin with a double-backslash, followed by some domain/IP, followed by a directory share (all of which can be mapped to another letter, for ease of access)

To access network shares, from Linux, you need to Mount the share to some location of the Linux tree.

You can look at many examples on-line, here is one: Mounting and mapping shares between Windows and Linux with Samba

Also, the resulting Path will look nothing like what you have for Windows, so, you will need to know (somehow) that you are running under which SO, and configure your paths accordingly.

Octavio Galindo
  • 330
  • 2
  • 9
1

Few suggestions

  1. Check the hosing environment & manipulate paths accordingly.
  2. You can use Path.Combine() to formulate your paths.

An example of this application is below

using System.Runtime.InteropServices;

class Program
    {
        static void Main(string[] args)
        {
            try {
                 bool validLogin = ValidateUser("domain",  "username", "password" );
                    if (validLogin)
                    {
                        var path = Path.Combine("\\\\10.123.123.123", "folder$", "subfolder");
                        string fullPath = Path.Combine(path, "file_name1");

                        if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                        {
                            path = path.Replace(@"\", @"/");
                            fullPath = fullPath.Replace(@"\", @"/");
                        }

                        string body = "Test File Contents";
                        if (!Directory.Exists(path))
                        {
                            Directory.CreateDirectory((path));
                        }
                        File.WriteAllText(fullPath, body);   
                    }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString() + ex.Message);
            }
        }

        public static bool ValidateUser(string domainName, string username, string password)
        {
            string userDn = $"{username}@{domainName}";
            try
            {
                using (var connection = new LdapConnection {SecureSocketLayer = false})
                {
                    connection.Connect(domainName, LdapConnection.DefaultPort);
                    connection.Bind(userDn, password);
                    if (connection.Bound)
                        return true;
                }
            }
            catch (LdapException )
            {
                // Log exception
            }
            return false;
        }
    }


Tejas Parnerkar
  • 201
  • 1
  • 5