Search code examples
c#apiasp.net-core.net-corefilestream

how to use network credential to connect share folder somewhere in the network?


this is my code and I want to know how to use network credentials in my code

 string filePath = Path.Combine(@"\\192.168.5.90\uploads", newfilename);                
     using (var filestream = new FileStream(filePath, FileMode.Create,FileAccess.Write))
     {
      await uploadfile.CopyToAsync(filestream);
     }
      return Ok(newfilename); 

Solution

  • you can use this link

    You can use a impersonator instead:

    using (var impersonator = new Impersonator(username, password))
    {
        File.Copy(source, destination, true);
    }
    

    this is a copy past from our implementation, so please adjust your domain name

    using System;
    using System.Runtime.InteropServices;
    using System.Security.Principal;
    
    public class Impersonator : IDisposable
    {
    /// <summary>
    ///     The Impersonator class is used to access a network share with other credentials.
    /// </summary>
    private readonly WindowsImpersonationContext _impersonatedUser;
    
    private readonly IntPtr _userHandle;
    
    /// <summary>
    ///     Constructor
    /// </summary>
    /// <param name="username">The user of the network share</param>
    /// <param name="password">The password of the network share</param>
    public Impersonator(string username, string password, string userDomain =   "YOURDOMAIN")
    {
        _userHandle = new IntPtr(0);
        bool returnValue = LogonUser(username, userDomain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
                                     ref _userHandle);
        if (!returnValue)
            throw new ApplicationException(
                "The applications wasn't able to impersonate the user with the specified credentials!");
        var newId = new WindowsIdentity(_userHandle);
        _impersonatedUser = newId.Impersonate();
    }
    
    #region IDisposable Members
    
    public void Dispose()
    {
        if (_impersonatedUser != null)
        {
            _impersonatedUser.Undo();
            CloseHandle(_userHandle);
        }
    }
    
    #endregion
    
    #region Interop imports/constants
    
    public const int LOGON32_LOGON_INTERACTIVE = 2;
    public const int LOGON32_LOGON_SERVICE = 3;
    public const int LOGON32_PROVIDER_DEFAULT = 0;
    
    [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
    public static extern bool LogonUser(String lpszUserName, String lpszDomain, String lpszPassword, int dwLogonType,
                                        int dwLogonProvider, ref IntPtr phToken);
    
    [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
    public static extern bool CloseHandle(IntPtr handle);
    
    #endregion
    }