Avatar of exalkonium
exalkonium
Flag for United States of America asked on

C# Manipulate files and folders on one server from another

Experts,

I am trying to programmatically create folders and copy files using C#. The code is sitting on a server in our domain, "app-server". The code uses a UNC path to try and recursively create folders at the moment, but I cannot create one folder as access is denied. I have tried impersonating the network administrator account, but that doesn't seem to do anything. I also tried giving rights of the folder to NETWORK SERVICE. Any advice on how to get this resolved?
sing System;
using System.Collections.Generic;
using System.Data;
using System.Data.Linq;
using System.Linq;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using CrmSdk;
using CRM;
using System.Security.Principal;
using System.Runtime.InteropServices;

public partial class networkrepository : System.Web.UI.Page
{
    const int LOGON32_LOGON_INTERACTIVE = 2;
    const int LOGON32_LOGON_NETWORK = 3;
    const int LOGON32_LOGON_BATCH = 4;
    const int LOGON32_LOGON_SERVICE = 5;
    const int LOGON32_LOGON_UNLOCK = 7;
    const int LOGON32_LOGON_NETWORK_CLEARTEXT = 8;
    const int LOGON32_LOGON_NEW_CREDENTIALS = 9;
    const int LOGON32_PROVIDER_DEFAULT = 0;

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern int LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, out IntPtr phToken);
    
    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern int ImpersonateLoggedOnUser(IntPtr hToken);

    [DllImport("advapi32.dll", SetLastError = true)]
    static extern int RevertToSelf();

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern int CloseHandle(IntPtr hObject);

    

    PFS_MSCRMEntities1 db = new PFS_MSCRMEntities1();


    protected void Page_Load(object sender, EventArgs e)
    {
        IntPtr token;
        int result = LogonUser("Administrator", ".", "mypassword", LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, out token);
        ImpersonateLoggedOnUser(token);

        IEnumerable<FilteredContact> contacts = (from a in db.FilteredContact
                                                 where a.statecodename == "Active"
                                                 select a);
        string targetPath = @"\\pfsfa-server\pfsdata\Physicians Financial Services\Clients\";

        foreach (FilteredContact contact in contacts)
        {
            string newPath = targetPath + contact.contactid.ToString();
            System.IO.Directory.CreateDirectory(newPath);
        }
        RevertToSelf();
        CloseHandle(token);
    }
}

Open in new window

Capture.PNG
C#Microsoft IIS Web ServerASP.NET

Avatar of undefined
Last Comment
vora_bhaumik

8/22/2022 - Mon
iHadi

I think a more mind peace solution is to use webservices.

Write a webservice method that takes a byte array as a parameter and saves it to disk.

Install this webservice on the server. Use it from the client to store files.

This approach will override security problems, firewall/proxy limitations and can be used from anywhere on the network without the hassle of file sharing and unc
ASKER CERTIFIED SOLUTION
vora_bhaumik

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck