post images

1

I am developing a website link in which I have a page that uploads images to a server of Windows Server type.

The folder where I want the images to be saved is in the same one that the project is located in, that is to say. C:\Users\adminyo\proyectosdevisual\img.... The adminyo folder is the one that is shared with me to administer since it is not my server and what I want is to paste the images in that place, but I do not know how to give it permission through C # code (it is with what I developed the page) so that it allows me to save files because when I save it comes out

  

Access to the path 'C: \ Users \ adminyo \ visualprojects \ img ...' is denied.

All this I do in the following way

 string filepath = "C:\Users\adminyo\proyectosdevisual\img...;
 HttpFileCollection uploadedFiles = Request.Files;
 Span1.Text = string.Empty;

 for (int i = 0; i < uploadedFiles.Count; i++)
 {
     HttpPostedFile userPostedFile = uploadedFiles[i];

     try
     {
         if (userPostedFile.ContentLength > 0)
         {
             Span1.Text += "<u>Imagen #" + (i + 1) + "</u><br>";
             Span1.Text += "Tipo de imagen: " + userPostedFile.ContentType + "<br>";
             Span1.Text += "Tamaño de imagen: " + userPostedFile.ContentLength + "kb<br>";
             Span1.Text += "Nombre de la imagen: " + userPostedFile.FileName + "<br>";

             userPostedFile.SaveAs(filepath + "\" + Path.GetFileName(userPostedFile.FileName));
             Span1.Text += "Se guardó en: " + filepath + "\" + Path.GetFileName(userPostedFile.FileName) + "<p>";
         }
     }
     catch (Exception Ex)
     {
         Span1.Text += "Error: <br>" + Ex.Message;
     }
 }

The folder that has the permissions is adminyo what I do not want is to move the permissions of it, rather I would like to find a way to access it since I have the credentials, since when I enter the server I do it remotely with my credentials but what I want is to be able to save the images through my application. How to indicate to the server that the application is authorized to save images?

    
asked by Darian25 06.09.2018 в 21:10
source

1 answer

2

You have to give permission to ISS USER . or failing that, impersonalize the process using your credentials, well that's the long way.

Edition 1: Impersonalization at the code level:

first a class is created that calls the native login methods:

public class Impersonator: IDisposable
    {
        private WindowsImpersonationContext impersonationContext = null;
        public WindowsIdentity tempWindowsIdentity { get; set; }
        public IntPtr token = IntPtr.Zero;

        public Impersonator(string userName, string domainName, string password)
        {
            ImpersonateValidUser(userName, domainName, password);
        }

        public void Dispose()
        {
            UndoImpersonation();
        }

        [DllImport("advapi32.dll", SetLastError = true)]
        private static extern int LogonUser(
            string lpszUserName,
            string lpszDomain,
            string lpszPassword,
            int dwLogonType,
            int dwLogonProvider,
            ref IntPtr phToken);

        [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern int DuplicateToken(
            IntPtr hToken,
            int impersonationLevel,
            ref IntPtr hNewToken);

        [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern bool RevertToSelf();

        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        private static extern bool CloseHandle(IntPtr handle);

        private const int LOGON32_LOGON_INTERACTIVE = 2;
        private const int LOGON32_PROVIDER_DEFAULT = 0;

        private void ImpersonateValidUser(string userName, string domain, string password)
        {
            tempWindowsIdentity = null;
            try
            {
                if (RevertToSelf())
                {
                    if (LogonUser(userName, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token) != 0)
                    {
                        tempWindowsIdentity = new WindowsIdentity(token);
                        impersonationContext = tempWindowsIdentity.Impersonate();
                    }
                    else
                    {
                        throw new Win32Exception(Marshal.GetLastWin32Error());
                    }
                }
                else
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }
            }
            finally
            {
                if (token != IntPtr.Zero)
                {
                    CloseHandle(token);
                }
            }
        }

        private void UndoImpersonation()
        {
            if (impersonationContext != null)
            {
                impersonationContext.Undo();
            }
        }
    }

Then in each code snippet you execute the following:

Everything out of context, will be executed with the service user, in the case of the IIS with IIS USER , and everything within the using will be made with the user that you indicate in the parameters usuario , dominio , contrasena

using (Impersonator imp = new Impersonator(usuario, dominio, contrasena))
{
  MethodoQueGuardaraTuArchivo();
}

and that's it.

    
answered by 06.09.2018 в 21:20