Hi,
I used the following code to do this. But this is unsafe code. Once i
specify the username and password and impoersonate, i am able to access the
files on the remote directories..
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
IntPtr tokenHandle = new IntPtr(0);
IntPtr dupeTokenHandle = new IntPtr(0);
try
{
string userName, domainName, passWord;
// Get the user token for the specified user, domain, and password using the
// unmanaged LogonUser method.
// The local machine name can be used for the domain name to impersonate a
user on this machine.
// Console.Write("Enter the name of the domain on which to log on: ");
domainName = tbxDomain.Text;
//Console.Write("Enter the login of a user on {0} that you wish to
impersonate: ", domainName);
userName = tbxUserName.Text;
passWord = tbxPassword.Text;
//Console.Write("Enter the password for {0}: ", userName);
const int LOGON32_PROVIDER_DEFAULT = 0;
//This parameter causes LogonUser to create a primary token.
const int LOGON32_LOGON_INTERACTIVE = 2;
tokenHandle = IntPtr.Zero;
// Call LogonUser to obtain a handle to an access token.
bool returnValue = LogonUser(userName, domainName, passWord,
LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
ref tokenHandle);
MessageBox.Show("Impersonating");
if (false == returnValue)
{
int ret = Marshal.GetLastWin32Error();
// Console.WriteLine("LogonUser failed with error code : {0}", ret);
MessageBox.Show("Login failed for" + userName);
throw new System.ComponentModel.Win32Exception(ret);
}
//Console.WriteLine("Did LogonUser Succeed? " + (returnValue? "Yes" :
"No"));
//Console.WriteLine("Value of Windows NT token: " + tokenHandle);
// Check the identity.
//Console.WriteLine("Before impersonation: "
// + WindowsIdentity.GetCurrent().Name);
MessageBox.Show(WindowsIdentity.GetCurrent().Name);
// Use the token handle returned by LogonUser.
WindowsIdentity newId = new WindowsIdentity(tokenHandle);
WindowsImpersonationContext impersonatedUser = newId.Impersonate();
// Check the identity.
//Console.WriteLine("After impersonation: "
MessageBox.Show(WindowsIdentity.GetCurrent().Name);
// Stop impersonating the user.
//impersonatedUser.Undo();
// Check the identity.
// Console.WriteLine("After Undo: " + WindowsIdentity.GetCurrent().Name);
//MessageBox.Show(WindowsIdentity.GetCurrent().Name);
// Free the tokens.
if (tokenHandle != IntPtr.Zero)
CloseHandle(tokenHandle);
}
catch (Exception ex)
{
//Console.WriteLine("Exception occurred. " + ex.Message);
MessageBox.Show(ex.Message);
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
On Wed, Nov 10, 2010 at 11:32 AM, Vivek Shantharam <[email protected]>wrote:
> Hi,
>
> I require some pointers/thoughts on how to implement a feature. I have
> worked with authentication methods in asp.net/iis. But now i use a windows
> application. I will have to be able to open a folder on a remote machine and
> be able to copy/delete files over there. i.e using the \\servername\C$ .
> now i can do that using start--> run and type it and i provide the
> username/password and i am able to do it. but how can i do the same using my
> windows applicatin??? is there any way i can promplt the user for entering
> those details. i get an exceptin straightaway.
>
> basically asking about implementing authencation/authorisation in windows
> forms.
>
> gratefull if i get some pointers.
>
> regards,.
> vivek.
>