locked
MVC 3 C# web application got UnathorizedAccessException when using alias url with Https:// to access remote network file RRS feed

  • Question

  • User-1320476259 posted

    Hello,

    I have developed a web application which has a feature to upload image and to display the image in a web page as well as to download the image.

    I have my IIS admin to deploy it to the server and usually i used Server IP Address/Applicationname (exp: 192.168.0.59/MvcImage) to access my system. The images are stored in a remote shared network folder which has an IP Address. In order to connect to the network i used WNetAddConnection following the example in (https://www.c-sharpcorner.com/blogs/how-to-access-network-drive-using-c-sharp) because i keep on getting UnauthorizedAccessException. The solution above worked if i used IpAdress/aplicationName to access my web application. However, the same exception which is UnauthorizedAccessException occured when i try to access the same web page using an alias url that the IIS admin created for my web application (exp: Https://ProductionServer.com/MvcImage)

    Here is some of the codes for establishing connection to the network drive:

    (NetworkCredentials.cs)

    private int ConnectToShare(string remoteUnc, string username, string password, bool promptUser)
        {
            NETRESOURCE nr = new NETRESOURCE
            {
                dwType = RESOURCETYPE_DISK,
                lpRemoteName = remoteUnc
            };
    
            int result;
            if (promptUser)
            {
                result = WNetUseConnection(IntPtr.Zero, nr, "", "", CONNECT_INTERACTIVE | CONNECT_PROMPT, null, null, null);
            }
            else
            {
                result = WNetUseConnection(IntPtr.Zero, nr, password, username, 0, null, null, null);
            }
    
            //if (result != NO_ERROR)
            //{
            //    throw new Win32Exception(result);
            //}
            return result;
        }
    
    public int AccessCredentials(string remoteComputerName, string userName, string password)
        {
            RemoteComputerName = remoteComputerName;
            UserName = userName;
            Password = password;
    
            return this.ConnectToShare(this._remoteUncName, this.UserName, this.Password, false);
        }

    Here is the code to check if the credentials to access the network folder is valid and the code to retrieve the file from the network folder

    mgr.cs

    public bool CredentialIsValid()
        {
            NetworkCredentials credentials = new NetworkCredentials();
            bool valid = false;
    
            int status = credentials.AccessCredentials(Paths.networkPath, Paths.networkUsername, Paths.networkPassword);
            if (status == 0 || status == 1219)
            {
                valid = true;
            }
            return valid;
        }
    
        public FileViewModel GetFile(string fullPath)
        {
            FileViewModel file = new FileViewModel();
            if (CredentialIsValid())
            { 
                WebClient client = new WebClient();
    
                file.fileData = client.DownloadData(Path.GetFullPath(fullPath));
                file.fileName = Path.GetFileName(fullPath);
                file.fileType = Path.GetExtension(fullPath);
            }
    
            return file;
        }

    And here is the code in controller to execute the codes above

    public ActionResult DisplayImage(string fullPath)
        {
            FileViewModel file = mgr.GetFile(fullPath);
    
            if(file.fileData == null)
            {
                return View("Network_Drive_Error");
            }
            else
            {
                return File(file.fileData, file.fileType, file.fileName);
            }
        }

    I only got this problem when i used Https:// as my url. If i deleted the Https:// from the url, the image can be accessed (viewed/downloaded) like intended.

    I am so confused that i do not even know if this is problem with my coding or server configuration/IIS/DNS configuration as i am not really good in networking

    Any advice/help would be appreciated

    Wednesday, October 23, 2019 3:40 AM

All replies

  • User665608656 posted

    Hi Ressa_Panda,

    As far as I know, the windows auth has two mode : NTLM and kerberos.

    If you use the ip address to access the web application, it will use NTLM by default. If you use domain to access the web application, it will use kerberos by default.

    If you use the kerberos you may faced this access issue. The solution is you should add SPN setting for the domain.

    For more details ,you could refer to this article:

    Setting up Kerberos Authentication for a Website in IIS

    Best Regards,

    YongQing.

    Thursday, October 24, 2019 9:58 AM
  • User-1320476259 posted

    Hello YongQing,

    Thanks for the reply and sorry for the late reply,

    I did not get notified on this reply,

    So, using alias like https://something.something.com/AppName would uses Kerberos,

    Just to confirm things:

    All of the application functionality are working fine, but when i tried to access the Network Attached Storage which is the remote server that i talked about above i got this error saying Access to "Path" is denied. I am also using the ip address to access the NAS.

    So i could access the web application using https no error encountered,

    the problem occurred when i am trying to access the NAS. Would this answer still be relevant?

    Best Regards

    Ressa_Panda

    Wednesday, October 30, 2019 2:29 AM