 |
|
 |
Hi,
i have tried everything and i get the same error.
Tried this :
I'm loged on the domain "AAA" with a user "A" and trying to log on the domain "AAA" as "B". Error 1326
tried this also :
I'm loged on the domain "AAA" with a user "A". I have created a local account "M" and tried log on the domain "localhost" as "localhost\M".
any ideas ?
|
|
|
|
 |
|
 |
Hi,
I'm using Win 2000 Server and the sample application return:
LogonUser() failed with error code: 1314
and then:
DuplicateToken() failed with error code: 6
This error encurred only in a machine, in other machine works fine. I search the code on web and I think that I have to set some permission. But I don't know what.
Please help me, thanks Matteo
|
|
|
|
 |
|
 |
I'm sorry, setting the permission "Act as part of the operating system" works fine. Sorry again
|
|
|
|
 |
|
 |
How do you do that? Can you show me an example.
|
|
|
|
 |
|
 |
* Log in as Administrator
* Execute gpedit.msc
* Go to "Local Computer Policy\Computer Configuration\Windows Settings\Security Settings\Local Policies\User Rights Assignment" and add the account being impersonated to “Act as part of the operating system”.
|
|
|
|
 |
|
|
 |
|
 |
If you have a global policy that overrides this setting then it explains why you cannot get it to work.
|
|
|
|
 |
|
 |
i used this solution to impersonate a domain user, but can't do with a local account.
the problem is:
- i have a workgroup, has two machine (mac1 & mac2)
- i create a local account on (mac1) [username=ysa,password=123]
- i create a shared folder on (mac1) called "Images", and assign the local user [ysa] to it.
- i want to access this shared folder from (mac2)
Yossef Elnaggar
|
|
|
|
 |
|
 |
http://www.codeproject.com/csharp/cpimpersonation1.asp?forumid=15321&select=2128115&df=100#xx2128115xx
|
|
|
|
 |
|
|
 |
|
 |
The first call to ImpersonateUser() takes about 10 or more seconds, I assume this is because of the Import statements (I read an article that explained exactly why it takes so long but honestly didn't fully understand it). On the second call though, it's instantaneous. Is there a way to speed this process up or maybe Import the dlls when I start my application so that when I make the first call it doesn't get bogged down.
Thanks,
Chris
-- modified at 12:23 Tuesday 4th April, 2006
|
|
|
|
 |
|
 |
10 seconds seems a bit excessive..? Honestly, I was still learning .NET when I wrote this article over 3 years ago, so I'd recommend using Uwe Keim's code instead of mine. His code is much more up-to-date and easier to use. I'd be curious to know if you are still incurring the latency issue, so let me know. Thanks!
|
|
|
|
 |
|
|
 |
|
 |
Actually it's -- http://www.codeproject.com/csharp/ZetaImpersonator.asp
|
|
|
|
 |
|
|
 |
|
 |
Hi,
Is it possible to contain impersonation to a given AppDomain rather than
impersonation consuming the full process?
I have a service running under the local system account, but need to
impersonate the current user for some functionality (e.g. accessing the
current users network share). However I don't want the full service to
change it's access rights. I can spawn a seperate process (which
impersonates a specifed user) but this therefore consumes more memory.
My service needs to run as the Local System account (not a specified account
particular to the domain).
Any ideas?
Thanks
Chris
|
|
|
|
 |
|
 |
Hi,
Even i have the same query..
I also want to impersonate a user (i'hav his credentials) from inside a service running under local system account so that i can access "Mapped Drives" that are mapped in the context of the user whom i want to impersonate.
I gave several experiments to this.. like trying and get the UNC name from the mapped name using WNet APIs but no results..
FYI: I can easily access network shares (using UNC names) to which i had access to. But couldnot access mapped drives.
Reason i'm aware of, that drives are mapped on a per user basis i.e. i cannot see a drive mapped in some another user's account.
But there must be some workaround.. like impersonating.. (but that too did not help..
If someone has a diffenent vision.. pls let us know..
Love,
TJ
|
|
|
|
 |
|
 |
Here you go...
public class NetworkHelper {
// Used to Map UNC from a Windows Service
#region Constants
//NetResource Scope
private const int RESOURCE_CONNECTED = 0x00000001;
private const int RESOURCE_GLOBALNET = 0x00000002;
private const int RESOURCE_REMEMBERED = 0x00000003;
//NetResource Type
private const int RESOURCETYPE_ANY = 0x00000000;
private const int RESOURCETYPE_DISK = 0x00000001;
private const int RESOURCETYPE_PRINT = 0x00000002;
//NetResource Usage
private const int RESOURCEUSAGE_CONNECTABLE = 0x00000001;
private const int RESOURCEUSAGE_CONTAINER = 0x00000002;
//NetResource Display Type
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;
//Flags
private const int CONNECT_UPDATE_PROFILE = 0x00000001;
private const int CONNECT_UPDATE_RECENT = 0x00000002;
private const int CONNECT_TEMPORARY = 0x00000004;
private const int CONNECT_INTERACTIVE = 0x00000008;
private const int CONNECT_PROMPT = 0x00000010;
private const int CONNECT_NEED_DRIVE = 0x00000020;
#endregion
#region NetResource Structure
[StructLayout(LayoutKind.Sequential)]
private struct NetResource {
public int Scope;
public int Type;
public int DisplayType;
public int Usage;
public string LocalName;
public string RemoteName;
public string Comment;
public string Provider;
}
#endregion
#region Win32 Functions
[DllImport("mpr.dll", EntryPoint = "WNetAddConnection2A", CharSet = CharSet.Ansi, SetLastError = true)]
private static extern int WNetAddConnection2A(ref NetResource netresource, string password, string username, int flags);
[DllImport("mpr.dll", EntryPoint = "WNetCancelConnection2", CharSet = CharSet.Ansi, SetLastError = true)]
private static extern int WNetCancelConnection2(string drivename, int flag, bool force);
#endregion
myLib lib = new myLib();
public bool WNetAddConnection(string LocalDrive, string NetworkFolderPath, string User, string Password, bool Force) {
bool success = false;
try {
NetResource netresource = new NetResource();
netresource.Scope = RESOURCE_GLOBALNET;
netresource.Type = RESOURCETYPE_DISK;
netresource.Usage = RESOURCEUSAGE_CONNECTABLE;
netresource.DisplayType = RESOURCEDISPLAYTYPE_SHARE;
netresource.LocalName = LocalDrive;
netresource.RemoteName = NetworkFolderPath;
netresource.Comment = "";
netresource.Provider = "";
int Flag = CONNECT_UPDATE_PROFILE;
if (Force) {
success = WNetCancelConnection(LocalDrive, true);
}
int result = WNetAddConnection2A(ref netresource, Password, User, Flag);
if (result > 0) {
throw new System.ComponentModel.Win32Exception(result);
}
success = true;
}
catch (Exception e) {
lib.Echo("Error: " + e.Message, myLib.MsgType.FAIL);
}
return success;
}
public bool WNetCancelConnection(string LocalDrive, bool Force) {
bool success = false;
try {
int result = WNetCancelConnection2(LocalDrive, CONNECT_UPDATE_PROFILE, Force);
if (result > 0) {
throw new System.ComponentModel.Win32Exception(result);
}
success = true;
}
catch (Exception e) {
lib.Echo("Error:" + e.Message, myLib.MsgType.FAIL);
}
return success;
}
}
|
|
|
|
 |
|
 |
Hi guys,
i need ur help on the impersonation when accessing the network share drive.
here's the scenario:
I have 3 file servers in windows domain which are f1.domain.local, f2.domain.local, f3.domain.local
for user with the PC not joining to the domain, every time they try to access the the file server they'll need to login into each server with the same credential. I found that rather annoying and would like to write a simple app that would do the job.
the above impersonation code works only on user account reside in the local machine, is there anyway i would be able to do impersonation while the user account reside in the domain. also trying to login into three file servers.
Regards,
Godwin
|
|
|
|
 |
|
 |
You might want to review this with an eye to coding style. For example:
IntPtr pExistingTokenHandle = new IntPtr(0);
IntPtr pDuplicateTokenHandle = new IntPtr(0);
pExistingTokenHandle = IntPtr.Zero;
pDuplicateTokenHandle = IntPtr.Zero;
...would be better written as:
IntPtr existingTokenHandle = IntPtr.Zero;
IntPtr duplicateTokenHandle = IntPtr.Zero;
The coding guidelines suggest not using Hungarian notation. It is sometimes a hard call to make when dealing with interop code, but I find it is clearer without the prefixes. In this case, the correct Hungarian prefix would be "h" (as in hExistingToken) anyway - the variable is not a pointer to a handle, it is a handle.
There is no point in initializing the IntPtr only to immediately set it again to the same value.
Some of the conditionals could be written in a clearer way:
if (sDomain == "")
...is correct, but the preferred idiom (for performance reasons) is:
if (domain.Length == 0)
Similarly, comparing against a boolean obscures that is going on:
if (false == bImpersonated)
...is clearer written as:
if (!impersonated)
I'm not sure that the point of the catch clause is - it doesn't appear to have any effect, and would be better to simply remove it:
catch (Exception ex)
{
throw ex;
}
Andy
|
|
|
|
 |
|
 |
Andy Neilson wrote:
I'm not sure that the point of the catch clause is - it doesn't appear to have any effect, and would be better to simply remove it:
catch (Exception ex)
{
throw ex;
}
This has the effect that you are loosing the most important part of the stack trace! So if you need to catch an exception for clean up/rollback purpose only then I highly recommend to rethrow the exception like that:
catch(Exception)
{
throw;
}
That's btw the only way how you can rethrow an exception.
cu
Max
"You can always improve your chances of writing
bug-free code by writing code that doesn't do anything"
Rob Macdonald, Serious ADO
|
|
|
|
 |
|
 |
I understand the difference between the two forms of throw statement, but I don't see why that catch clause is there at all. You aren't handling the exception in the catch clause, and any cleanup is dealt with in the finally clause. Just using a try-finally (i.e., without the catch) is simpler.
Andy
|
|
|
|
 |
|
 |
Andy Neilson wrote:
I understand the difference between the two forms of throw statement
I don't doubt that, Andy! I only wanted to emphasise the difference in the context of the article.
Andy Neilson wrote:
but I don't see why that catch clause is there at all
The only one who knows is probably the author itself...
Max
"You can always improve your chances of writing
bug-free code by writing code that doesn't do anything"
Rob Macdonald, Serious ADO
|
|
|
|
 |
|
|
 |
|
 |
Just curious, but what is your contribution to this topic? First off, it was not copied verbatim - there is an actual Windows Form front-end to the code.
|
|
|
|
 |