* For benefit of all facing similar problem-for File.Copy in Lan(Use Shared Resource)
For this i impersonated the user who can access the network.*
public const int LOGON32_LOGON_INTERACTIVE = 2;
public const int LOGON32_PROVIDER_DEFAULT = 0;
WindowsImpersonationContext impersonationContext;
[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)]
public static extern int DuplicateToken(IntPtr hToken,
int impersonationLevel,
ref IntPtr hNewToken);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool RevertToSelf();
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern bool CloseHandle(IntPtr handle);
* protected void Page_Load(object sender, EventArgs e)*
if (impersonateValidUser("XXXXXXX", "XXXX", "XXXX"))
{
//File.Copy(@"\\XXX\XX.XX", @"\\YYYY\YY\YY.YY");
undoImpersonation();
}
else
{
//Your impersonation failed. Therefore, include a fail-safe
mechanism here.
}
private bool impersonateValidUser(String userName, String domain, String
password)
{
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
if (RevertToSelf())
{
if (LogonUserA(userName, domain, password,
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;
}
private void undoImpersonation()
{
impersonationContext.Undo();
}
[?][?][?][?]
Pratiksha
On Fri, Nov 20, 2009 at 2:13 PM, Raghupathi Kamuni <[email protected]>wrote:
> How? Let us know the solution. It would be helpful for those looking for
> solution facing similar problems.
>
>
> On Fri, Nov 20, 2009 at 10:55 AM, Pratiksha Saxena <
> [email protected]> wrote:
>
>> Finally i solved this.
>>
>>
>> Thanks to all for ur suggestions
>>
>> Pratiksha
>>
>>
>> On Wed, Nov 18, 2009 at 9:50 PM, slim MOURALI <[email protected]>wrote:
>>
>>> permission in IIS server
>>>
>>> 2009/11/18 Pratiksha Saxena <[email protected]>
>>>
>>> What permission should i give.I have already given permission in folder
>>>> for the files to be copied to the user.
>>>> Eg my machine name is okh-lb-labs-014 i have given rights
>>>> to okh-lb-labs-014 as full control.What else should i give.
>>>>
>>>> Thanks
>>>> Pratiksha
>>>>
>>>>
>>>> On Tue, Nov 17, 2009 at 10:02 PM, slim MOURALI
>>>> <[email protected]>wrote:
>>>>
>>>>> you must give permission on the remote machine... to the user that the
>>>>> iis
>>>>> is running
>>>>>
>>>>> 2009/11/17 Pratiksha Saxena <[email protected]>
>>>>>
>>>>> Hi,
>>>>>>
>>>>>> i have a web application in which on button_click i have written code
>>>>>> for File.Copy
>>>>>>
>>>>>> File.Copy(@"e:\Text.txt", @"\\okh-lb-labs-030\\D$\\MT\\Text.txt");
>>>>>>
>>>>>>
>>>>>> but i gives error *Logon failure: unknown user name or bad password.*
>>>>>> *
>>>>>> *
>>>>>> *The files are in Intranet.*
>>>>>> Source is e:\Text.txt
>>>>>> Destination is \okh-lb-labs-030\\D$\\MT\\Text.txt
>>>>>>
>>>>>> If i copy on my local system it is fine but on network it gives
>>>>>> error.i tried even giving rights to MT folder but no use.
>>>>>>
>>>>>> Please help.
>>>>>>
>>>>>> Pratiksha
>>>>>>
>>>>>
>>>>>
>>>>
>>>
>>
>
<<330.gif>>
