Upload File on Network Path

Lakhangarg
Posted by Lakhangarg under ASP.NET category on | Views : 7469
Below Code Snippet is used to upload the file on the system in the network.
this can be achieved with the help of impersonation.
WindowsImpersonationContext impersonationContext;
Code to get User Name,Domain Name,Domain Password From Web.config File:

ConfigurationManager.AppSettings["DomainUser"].ToString()
ConfigurationManager.AppSettings["Domain"].ToString() ConfigurationManager.AppSettings["DomainPassword"].ToString()


LogonUserA Internal Function is used to login in as a user and get the token.
LOGON32_LOGON_INTERACTIVE:This logon type is intended for users who will be interactively using the machine.
LOGON32_PROVIDER_DEFAULT:Use the standard logon provider for the system.
UploadFile Method Take two Parameters: one Ref Parameter to Uploaf file control and second is the path where we want to upload image.
UploadFile call the Method "impersonateValidUser()" to impersonate tha valid user and Get Token to upload file.

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Security.Principal;
using System.Runtime.InteropServices;


public class FileHandler
{
public const int LOGON32_LOGON_INTERACTIVE = 2;
public const int LOGON32_PROVIDER_DEFAULT = 0;

WindowsImpersonationContext impersonationContext;

//Programatically Add Service to Get Token
[DllImport("advapi32.dll")]
public static extern int LogonUserA(String lpszUserName,
String lpszDomain,
String lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]

//Programatically Add Service to Check for Duplicate Token
public static extern int DuplicateToken(IntPtr hToken,
int impersonationLevel,
ref IntPtr hNewToken);

//RevertToSelf terminates the impersonation of a client application.
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool RevertToSelf();

//Programatically Add Service to Terminate Process
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern bool CloseHandle(IntPtr handle);

public bool impersonateValidUser()
{
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;

if (RevertToSelf())
{

if (LogonUserA(ConfigurationManager.AppSettings["DomainUser"].ToString(), ConfigurationManager.AppSettings["Domain"].ToString(), ConfigurationManager.AppSettings["DomainPassword"].ToString(), LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT, ref token) != 0)
{
if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
{
tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
impersonationContext = tempWindowsIdentity.Impersonate();
if (impersonationContext != null)
{
CloseHandle(token);
CloseHandle(tokenDuplicate);
return true;
}
}
}
}
if (token != IntPtr.Zero)
CloseHandle(token);
if (tokenDuplicate != IntPtr.Zero)
CloseHandle(tokenDuplicate);
return false;
}

public Boolean UploadFile(ref FileUpload flUpload,string NetworkPath)
{
if (impersonateValidUser())
{
flUpload.SaveAs(NetworkPath + flUpload.FileName);
return true;
}
else
{
return false;
}
}
}

Comments or Responses

Login to post response