Domanda

Ci siamo imbattuti in una situazione interessante che deve essere risolta e le mie ricerche si sono rivelate nulle. Chiedo quindi aiuto alla comunità degli SO.

Il problema è questo: abbiamo bisogno di accedere a livello di codice a un file condiviso che non è nel nostro dominio e non si trova all'interno di un dominio esterno attendibile tramite la condivisione di file / UNC remoti. Naturalmente, dobbiamo fornire le credenziali al computer remoto.

In genere, si risolve questo problema in due modi:

  1. Mappa la condivisione file come unità e fornisci le credenziali in quel momento. Questo in genere viene fatto utilizzando il comando NET USE o le funzioni Win32 che duplicano NET USE .
  2. Accedi al file con un percorso UNC come se il computer remoto fosse nel dominio e assicurati che l'account con cui viene eseguito il programma sia duplicato (inclusa la password) sul computer remoto come utente locale. Fondamentalmente sfrutta il fatto che Windows fornirà automaticamente le credenziali dell'utente corrente quando l'utente tenta di accedere a un file condiviso.
  3. Non utilizzare la condivisione file remota. Utilizzare FTP (o altri mezzi) per trasferire il file, lavorarci su localmente, quindi trasferirlo nuovamente.

Per vari e vari motivi, i nostri architetti di sicurezza / rete hanno rifiutato i primi due approcci. Il secondo approccio è ovviamente un buco nella sicurezza; se il computer remoto è compromesso, il computer locale è ora a rischio. Il primo approccio non è soddisfacente perché l'unità appena montata è una risorsa condivisa disponibile per altri programmi sul computer locale durante l'accesso ai file da parte del programma. Anche se è del tutto possibile rendere questo temporaneo, è ancora un buco nella loro opinione.

Sono aperti alla terza opzione, ma gli amministratori della rete remota insistono su SFTP anziché su FTPS e FtpWebRequest supporta solo FTPS. SFTP è l'opzione più adatta ai firewall e ci sono un paio di librerie che potrei usare per quell'approccio, ma preferirei ridurre le mie dipendenze se posso.

Ho cercato in MSDN un mezzo gestito o win32 per utilizzare la condivisione file remota, ma non sono riuscito a trovare qualcosa di utile.

E così chiedo: c'è un altro modo? Mi sono perso una funzione win32 super segreta che fa quello che voglio? O devo perseguire una variante dell'opzione 3?

È stato utile?

Soluzione

Il modo per risolvere il tuo problema è usare un'API Win32 chiamata WNetUseConnection .
Utilizzare questa funzione per connettersi a un percorso UNC con autenticazione, NON per mappare un'unità .

Ciò ti consentirà di connetterti a un computer remoto, anche se non si trova sullo stesso dominio e anche se ha un nome utente e una password diversi.

Dopo aver usato WNetUseConnection sarai in grado di accedere al file tramite un percorso UNC come se fossi sullo stesso dominio. Il modo migliore è probabilmente attraverso le condivisioni amministrative integrate.
Esempio: \\ nomecomputer \ c $ \ programmi \ Cartella \ file.txt

Ecco alcuni esempi di codice C # che utilizza WNetUseConnection .

Nota, per NetResource, dovresti passare null per lpLocalName e lpProvider. Il tipo dwT dovrebbe essere RESOURCETYPE_DISK. LpRemoteName dovrebbe essere \\ ComputerName.

Altri suggerimenti

Per le persone che cercano una soluzione rapida, è possibile utilizzare NetworkShareAccesser che ho scritto di recente (basato su questo rispondi (grazie mille!)):

Utilizzo:

using (NetworkShareAccesser.Access(REMOTE_COMPUTER_NAME, DOMAIN, USER_NAME, PASSWORD))
{
    File.Copy(@"C:\Some\File\To\copy.txt", @"\\REMOTE-COMPUTER\My\Shared\Target\file.txt");
}

ATTENZIONE: Assicurati assolutamente che venga chiamato Dispose del NetworkShareAccesser (anche se l'app si arresta in modo anomalo!), altrimenti si apre la connessione rimarrà su Windows. Puoi vedere tutte le connessioni aperte aprendo il prompt cmd e inserisci net use .

Il codice:

/// <summary>
/// Provides access to a network share.
/// </summary>
public class NetworkShareAccesser : IDisposable
{
    private string _remoteUncName;
    private string _remoteComputerName;

    public string RemoteComputerName
    {
        get
        {
            return this._remoteComputerName;
        }
        set
        {
            this._remoteComputerName = value;
            this._remoteUncName = @"\\" + this._remoteComputerName;
        }
    }

    public string UserName
    {
        get;
        set;
    }
    public string Password
    {
        get;
        set;
    }

    #region Consts

    private const int RESOURCE_CONNECTED = 0x00000001;
    private const int RESOURCE_GLOBALNET = 0x00000002;
    private const int RESOURCE_REMEMBERED = 0x00000003;

    private const int RESOURCETYPE_ANY = 0x00000000;
    private const int RESOURCETYPE_DISK = 0x00000001;
    private const int RESOURCETYPE_PRINT = 0x00000002;

    private const int RESOURCEDISPLAYTYPE_GENERIC = 0x00000000;
    private const int RESOURCEDISPLAYTYPE_DOMAIN = 0x00000001;
    private const int RESOURCEDISPLAYTYPE_SERVER = 0x00000002;
    private const int RESOURCEDISPLAYTYPE_SHARE = 0x00000003;
    private const int RESOURCEDISPLAYTYPE_FILE = 0x00000004;
    private const int RESOURCEDISPLAYTYPE_GROUP = 0x00000005;

    private const int RESOURCEUSAGE_CONNECTABLE = 0x00000001;
    private const int RESOURCEUSAGE_CONTAINER = 0x00000002;


    private const int CONNECT_INTERACTIVE = 0x00000008;
    private const int CONNECT_PROMPT = 0x00000010;
    private const int CONNECT_REDIRECT = 0x00000080;
    private const int CONNECT_UPDATE_PROFILE = 0x00000001;
    private const int CONNECT_COMMANDLINE = 0x00000800;
    private const int CONNECT_CMD_SAVECRED = 0x00001000;

    private const int CONNECT_LOCALDRIVE = 0x00000100;

    #endregion

    #region Errors

    private const int NO_ERROR = 0;

    private const int ERROR_ACCESS_DENIED = 5;
    private const int ERROR_ALREADY_ASSIGNED = 85;
    private const int ERROR_BAD_DEVICE = 1200;
    private const int ERROR_BAD_NET_NAME = 67;
    private const int ERROR_BAD_PROVIDER = 1204;
    private const int ERROR_CANCELLED = 1223;
    private const int ERROR_EXTENDED_ERROR = 1208;
    private const int ERROR_INVALID_ADDRESS = 487;
    private const int ERROR_INVALID_PARAMETER = 87;
    private const int ERROR_INVALID_PASSWORD = 1216;
    private const int ERROR_MORE_DATA = 234;
    private const int ERROR_NO_MORE_ITEMS = 259;
    private const int ERROR_NO_NET_OR_BAD_PATH = 1203;
    private const int ERROR_NO_NETWORK = 1222;

    private const int ERROR_BAD_PROFILE = 1206;
    private const int ERROR_CANNOT_OPEN_PROFILE = 1205;
    private const int ERROR_DEVICE_IN_USE = 2404;
    private const int ERROR_NOT_CONNECTED = 2250;
    private const int ERROR_OPEN_FILES = 2401;

    #endregion

    #region PInvoke Signatures

    [DllImport("Mpr.dll")]
    private static extern int WNetUseConnection(
        IntPtr hwndOwner,
        NETRESOURCE lpNetResource,
        string lpPassword,
        string lpUserID,
        int dwFlags,
        string lpAccessName,
        string lpBufferSize,
        string lpResult
        );

    [DllImport("Mpr.dll")]
    private static extern int WNetCancelConnection2(
        string lpName,
        int dwFlags,
        bool fForce
        );

    [StructLayout(LayoutKind.Sequential)]
    private class NETRESOURCE
    {
        public int dwScope = 0;
        public int dwType = 0;
        public int dwDisplayType = 0;
        public int dwUsage = 0;
        public string lpLocalName = "";
        public string lpRemoteName = "";
        public string lpComment = "";
        public string lpProvider = "";
    }

    #endregion

    /// <summary>
    /// Creates a NetworkShareAccesser for the given computer name. The user will be promted to enter credentials
    /// </summary>
    /// <param name="remoteComputerName"></param>
    /// <returns></returns>
    public static NetworkShareAccesser Access(string remoteComputerName)
    {
        return new NetworkShareAccesser(remoteComputerName);
    }

    /// <summary>
    /// Creates a NetworkShareAccesser for the given computer name using the given domain/computer name, username and password
    /// </summary>
    /// <param name="remoteComputerName"></param>
    /// <param name="domainOrComuterName"></param>
    /// <param name="userName"></param>
    /// <param name="password"></param>
    public static NetworkShareAccesser Access(string remoteComputerName, string domainOrComuterName, string userName, string password)
    {
        return new NetworkShareAccesser(remoteComputerName,
                                        domainOrComuterName + @"\" + userName,
                                        password);
    }

    /// <summary>
    /// Creates a NetworkShareAccesser for the given computer name using the given username (format: domainOrComputername\Username) and password
    /// </summary>
    /// <param name="remoteComputerName"></param>
    /// <param name="userName"></param>
    /// <param name="password"></param>
    public static NetworkShareAccesser Access(string remoteComputerName, string userName, string password)
    {
        return new NetworkShareAccesser(remoteComputerName, 
                                        userName,
                                        password);
    }

    private NetworkShareAccesser(string remoteComputerName)
    {
        RemoteComputerName = remoteComputerName;               

        this.ConnectToShare(this._remoteUncName, null, null, true);
    }

    private NetworkShareAccesser(string remoteComputerName, string userName, string password)
    {
        RemoteComputerName = remoteComputerName;
        UserName = userName;
        Password = password;

        this.ConnectToShare(this._remoteUncName, this.UserName, this.Password, false);
    }

    private void 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);
        }
    }

    private void DisconnectFromShare(string remoteUnc)
    {
        int result = WNetCancelConnection2(remoteUnc, CONNECT_UPDATE_PROFILE, false);
        if (result != NO_ERROR)
        {
            throw new Win32Exception(result);
        }
    }

    /// <summary>
    /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
    /// </summary>
    /// <filterpriority>2</filterpriority>
    public void Dispose()
    {
        this.DisconnectFromShare(this._remoteUncName);
    }
}

AFAIK, non è necessario mappare il percorso UNC a una lettera di unità per stabilire le credenziali per un server. Ho usato regolarmente script batch come:

net use \\myserver /user:username password

:: do something with \\myserver\the\file\i\want.xml

net use /delete \\my.server.com

Tuttavia, qualsiasi programma in esecuzione sullo stesso account del programma sarebbe comunque in grado di accedere a tutto ciò a cui username: password ha accesso. Una possibile soluzione potrebbe essere quella di isolare il programma nel proprio account utente locale (l'accesso UNC è locale all'account che ha chiamato NET USE ).

Nota: l'uso di SMB in tutti i domini non è un buon uso della tecnologia, IMO. Se la sicurezza è così importante, il fatto che SMB manchi di crittografia è un po 'più umido da solo.

Anziché WNetUseConnection, consiglierei NetUseAdd . WNetUseConnection è una funzione legacy sostituita da WNetUseConnection2 e WNetUseConnection3, ma tutte queste funzioni creano un dispositivo di rete visibile in Esplora risorse. NetUseAdd equivale a chiamare net use in un prompt DOS per l'autenticazione su un computer remoto.

Se si chiama NetUseAdd, i tentativi successivi di accesso alla directory dovrebbero avere esito positivo.

Anche se non conosco me stesso, spero sicuramente che la # 2 sia errata ... Mi piacerebbe pensare che Windows non fornirà AUTOMATICAMENTE le mie informazioni di accesso (soprattutto la mia password!) a qualsiasi macchina, per non parlare di quella che non fa parte della mia fiducia.

Indipendentemente da ciò, hai esplorato l'architettura della rappresentazione? Il tuo codice sarà simile a questo:

using (System.Security.Principal.WindowsImpersonationContext context = System.Security.Principal.WindowsIdentity.Impersonate(token))
{
    // Do network operations here

    context.Undo();
}

In questo caso, la variabile token è IntPtr. Per ottenere un valore per questa variabile, dovrai chiamare la funzione API Windows LogonUser non gestita. Un breve viaggio a pinvoke.net ci dà la seguente firma:

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

Nome utente, dominio e password dovrebbero apparire abbastanza ovvi. Dai un'occhiata ai vari valori che possono essere passati a dwLogonType e dwLogonProvider per determinare quello più adatto alle tue esigenze.

Questo codice non è stato testato, in quanto non ho un secondo dominio qui in cui posso verificare, ma si spera che questo ti porti sulla strada giusta.

La maggior parte dei server SFTP supporta anche SCP, che può essere molto più facile trovare librerie per. Potresti anche chiamare un client esistente dal tuo codice come pscp incluso in PuTTY .

Se il tipo di file con cui stai lavorando è qualcosa di semplice come un file di testo o XML, potresti persino arrivare a scrivere la tua implementazione client / server per manipolare il file usando qualcosa come .NET Remoting o web servizi.

Ho visto l'opzione 3 implementata con strumenti JScape in modo abbastanza semplice. Potresti provarlo. Non è gratuito, ma fa il suo lavoro.

Qui una classe POC minima con tutta l'innesto rimossa

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;

public class UncShareWithCredentials : IDisposable
{
    private string _uncShare;

    public UncShareWithCredentials(string uncShare, string userName, string password)
    {
        var nr = new Native.NETRESOURCE
        {
            dwType = Native.RESOURCETYPE_DISK,
            lpRemoteName = uncShare
        };

        int result = Native.WNetUseConnection(IntPtr.Zero, nr, password, userName, 0, null, null, null);
        if (result != Native.NO_ERROR)
        {
            throw new Win32Exception(result);
        }
        _uncShare = uncShare;
    }

    public void Dispose()
    {
        if (!string.IsNullOrEmpty(_uncShare))
        {
            Native.WNetCancelConnection2(_uncShare, Native.CONNECT_UPDATE_PROFILE, false);
            _uncShare = null;
        }
    }

    private class Native
    {
        public const int RESOURCETYPE_DISK = 0x00000001;
        public const int CONNECT_UPDATE_PROFILE = 0x00000001;
        public const int NO_ERROR = 0;

        [DllImport("mpr.dll")]
        public static extern int WNetUseConnection(IntPtr hwndOwner, NETRESOURCE lpNetResource, string lpPassword, string lpUserID,
            int dwFlags, string lpAccessName, string lpBufferSize, string lpResult);

        [DllImport("mpr.dll")]
        public static extern int WNetCancelConnection2(string lpName, int dwFlags, bool fForce);

        [StructLayout(LayoutKind.Sequential)]
        public class NETRESOURCE
        {
            public int dwScope;
            public int dwType;
            public int dwDisplayType;
            public int dwUsage;
            public string lpLocalName;
            public string lpRemoteName;
            public string lpComment;
            public string lpProvider;
        }
    }
}

Puoi utilizzare direttamente \\ server \ share \ folder w / WNetUseConnection , non è necessario rimuoverlo solo nella parte \\ server in precedenza.

im allego il mio codice vb.net basato su brian riferimento

Imports System.ComponentModel

Imports System.Runtime.InteropServices

Public Class PinvokeWindowsNetworking

Const NO_ERROR As Integer = 0



Private Structure ErrorClass

    Public num As Integer

    Public message As String



    Public Sub New(ByVal num As Integer, ByVal message As String)

        Me.num = num

        Me.message = message

    End Sub

End Structure



Private Shared ERROR_LIST As ErrorClass() = New ErrorClass() {

    New ErrorClass(5, "Error: Access Denied"),

    New ErrorClass(85, "Error: Already Assigned"),

    New ErrorClass(1200, "Error: Bad Device"),

    New ErrorClass(67, "Error: Bad Net Name"),

    New ErrorClass(1204, "Error: Bad Provider"),

    New ErrorClass(1223, "Error: Cancelled"),

    New ErrorClass(1208, "Error: Extended Error"),

    New ErrorClass(487, "Error: Invalid Address"),

    New ErrorClass(87, "Error: Invalid Parameter"),

    New ErrorClass(1216, "Error: Invalid Password"),

    New ErrorClass(234, "Error: More Data"),

    New ErrorClass(259, "Error: No More Items"),

    New ErrorClass(1203, "Error: No Net Or Bad Path"),

    New ErrorClass(1222, "Error: No Network"),

    New ErrorClass(1206, "Error: Bad Profile"),

    New ErrorClass(1205, "Error: Cannot Open Profile"),

    New ErrorClass(2404, "Error: Device In Use"),

    New ErrorClass(2250, "Error: Not Connected"),

    New ErrorClass(2401, "Error: Open Files")}



Private Shared Function getErrorForNumber(ByVal errNum As Integer) As String

    For Each er As ErrorClass In ERROR_LIST

        If er.num = errNum Then Return er.message

    Next



    Try

        Throw New Win32Exception(errNum)

    Catch ex As Exception

        Return "Error: Unknown, " & errNum & " " & ex.Message

    End Try



    Return "Error: Unknown, " & errNum

End Function



<DllImport("Mpr.dll")>

Private Shared Function WNetUseConnection(ByVal hwndOwner As IntPtr, ByVal lpNetResource As NETRESOURCE, ByVal lpPassword As String, ByVal lpUserID As String, ByVal dwFlags As Integer, ByVal lpAccessName As String, ByVal lpBufferSize As String, ByVal lpResult As String) As Integer

End Function



<DllImport("Mpr.dll")>

Private Shared Function WNetCancelConnection2(ByVal lpName As String, ByVal dwFlags As Integer, ByVal fForce As Boolean) As Integer

End Function



<StructLayout(LayoutKind.Sequential)>

Private Class NETRESOURCE

    Public dwScope As Integer = 0

    Public dwType As Integer = 0

    Public dwDisplayType As Integer = 0

    Public dwUsage As Integer = 0

    Public lpLocalName As String = ""

    Public lpRemoteName As String = ""

    Public lpComment As String = ""

    Public lpProvider As String = ""

End Class



Public Shared Function connectToRemote(ByVal remoteUNC As String, ByVal username As String, ByVal password As String) As String

    Return connectToRemote(remoteUNC, username, password, False)

End Function



Public Shared Function connectToRemote(ByVal remoteUNC As String, ByVal username As String, ByVal password As String, ByVal promptUser As Boolean) As String

    Dim nr As NETRESOURCE = New NETRESOURCE()

    nr.dwType = ResourceTypes.Disk

    nr.lpRemoteName = remoteUNC

    Dim ret As Integer



    If promptUser Then

        ret = WNetUseConnection(IntPtr.Zero, nr, "", "", Connects.Interactive Or Connects.Prompt, Nothing, Nothing, Nothing)

    Else

        ret = WNetUseConnection(IntPtr.Zero, nr, password, username, 0, Nothing, Nothing, Nothing)

    End If



    If ret = NO_ERROR Then Return Nothing

    Return getErrorForNumber(ret)

End Function



Public Shared Function disconnectRemote(ByVal remoteUNC As String) As String

    Dim ret As Integer = WNetCancelConnection2(remoteUNC, Connects.UpdateProfile, False)

    If ret = NO_ERROR Then Return Nothing

    Return getErrorForNumber(ret)

End Function


Enum Resources As Integer

    Connected = &H1

    GlobalNet = &H2

    Remembered = &H3

End Enum


Enum ResourceTypes As Integer

    Any = &H0

    Disk = &H1

    Print = &H2

End Enum


Enum ResourceDisplayTypes As Integer

    Generic = &H0

    Domain = &H1

    Server = &H2

    Share = &H3

    File = &H4

    Group = &H5

End Enum


Enum ResourceUsages As Integer

    Connectable = &H1

    Container = &H2

End Enum


Enum Connects As Integer

    Interactive = &H8

    Prompt = &H10

    Redirect = &H80

    UpdateProfile = &H1

    CommandLine = &H800

    CmdSaveCred = &H1000

    LocalDrive = &H100

End Enum


End Class

come usarlo

Dim login = PinvokeWindowsNetworking.connectToRemote("\\ComputerName", "ComputerName\UserName", "Password")

    If IsNothing(login) Then



        'do your thing on the shared folder



       PinvokeWindowsNetworking.disconnectRemote("\\ComputerName")

    End If

Ho cercato MS per trovare le risposte. La prima soluzione presuppone che l'account utente che esegue il processo dell'applicazione abbia accesso alla cartella o all'unità condivisa (stesso dominio). Assicurati che il tuo DNS sia stato risolto o prova a utilizzare l'indirizzo IP. Fai semplicemente quanto segue:

 DirectoryInfo di = new DirectoryInfo(PATH);
 var files = di.EnumerateFiles("*.*", SearchOption.AllDirectories);

Se si desidera attraversare diversi domini .NET 2.0 con credenziali, seguire questo modello:

WebRequest req = FileWebRequest.Create(new Uri(@"\\<server Name>\Dir\test.txt"));

        req.Credentials = new NetworkCredential(@"<Domain>\<User>", "<Password>");
        req.PreAuthenticate = true;

        WebResponse d = req.GetResponse();
        FileStream fs = File.Create("test.txt");

        // here you can check that the cast was successful if you want. 
        fs = d.GetResponseStream() as FileStream;
        fs.Close();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top