|
|
Comments and Discussions
|
|
 |

|
I can't seem to get this to work on Windows Server 2012. I get the following throws an exception:
System.Collections.IList errors = null;
try
{
using ( new Impersonator ( "Administrator", "Domain", "Password" ) )
{
using ( RunspaceInvoke invoker = new RunspaceInvoke() )
{
invoker.Invoke("Set-ExecutionPolicy Unrestricted", null, out errors);
}
}
}
The exception: "Invalid token for impersonation - it cannot be duplicated."
Does this mean that the impersonator won't work on Windows Server 2012?
|
|
|
|
|

|
Really liked this one! And pietvredeveld solution its also great!
|
|
|
|

|
Using the class returns a catastrophic failure,
I'm using the code as follows,
private void btnOmzetten_Click(object sender, EventArgs e)
{
if(string.IsNullOrWhiteSpace(txtProfiel.Text))
{
MessageBox.Show("Het veld mag niet leeg zijn","Leeg",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
txtProfiel.Focus();
txtLog.Text = "Er is geen profiel ingevult";
}
else
{
using (new Impersonator(txtGebruiker.Text, txtDomein.Text, txtPw.Text))
{
string profile = GetProfilePath(txtProfiel.Text);
txtLog.Text = profile;
}
}
And the part where i get the catastrophic failure is :
private string GetProfilePath(string user)
{
DirectoryEntry dirEntry = CreateDirEntry(txtGebruiker.Text, txtPw.Text);
DirectorySearcher dirSearcher = new DirectorySearcher();
dirSearcher.Filter = "mail=" + user; dirSearcher.PropertiesToLoad.Add("profilePath"); SearchResult sResult = dirSearcher.FindOne();
if (sResult == null)
{
MessageBox.Show("Niks gevonden", "Fout", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
txtLog.Text = "Er is niks gevonden";
return null;
}
DirectoryEntry dirEntr = new DirectoryEntry(sResult.Path);
dirEntr.Username = txtGebruiker.Text;
dirEntr.Password = txtPw.Text;
string profile = dirEntr.Properties["profilePath"][0].ToString(); dirEntr.Close(); return profile;
}
I'm not sure what i'm doing wrong.
Any help or suggestions are welcome,
|
|
|
|

|
This is what I was looking for
|
|
|
|

|
not working for SharePoint 2010 code
|
|
|
|

|
Did you ever get it working? Is there a place to get the most current version of the program?
|
|
|
|

|
"Not working" is usually the worst error description one can think of. Ensures you get no help, guaranteed!
If you really want help, try following these rules [^].
Wollen Sie ganz einfach Ihre eigene Homepage erstellen, ohne HTML-Kenntnisse, einfach, professionell und mit viel Freude? Probieren Sie unser Desktop Content Management System (CMS) Zeta Producer für Windows aus. Komplett mit eigenem Shop, Gästebuch, Weblog, Bildergalerien, Integration von YouTube-Videos. Wir haben eine aktive Anwender-Community, schnellen Support, sympathische Support-Mitarbeiter.
|
|
|
|

|
Hi,
The original code has a lot of CA warnings and has some small memory leaks. I've improved the code regarding this issues,without changing the functionality. When interested contact me .
edit: Here is the code http://pastebin.com/EmKYDN6R[^]
Regards
Piet
modified 26 Apr '12 - 16:04.
|
|
|
|

|
Sure! You could simply link a pastebin.com here for others until I can include it.
Wollen Sie ganz einfach Ihre eigene Homepage erstellen, ohne HTML-Kenntnisse, einfach, professionell und mit viel Freude? Probieren Sie unser Desktop Content Management System (CMS) Zeta Producer für Windows aus. Komplett mit eigenem Shop, Gästebuch, Weblog, Bildergalerien, Integration von YouTube-Videos. Wir haben eine aktive Anwender-Community, schnellen Support, sympathische Support-Mitarbeiter.
|
|
|
|
|

|
Hi,
The code does not work (not impersonate) when I am trying to access a website from a computer located on a different network. Any suggestions?
Thanks in advance.
|
|
|
|

|
Very Nice Indeed
|
|
|
|

|
Thank you very much. I like the concise and clean code.
|
|
|
|

|
very very good job and nice and easy to use it
bravo from french programmer
|
|
|
|

|
This has solved problems for me multiple times
|
|
|
|

|
Hi when i am using the same code in Windows Server 2008 R2 it is giving me
Logon failure: the user has not been granted the requested logon type at this computer
where as it is working in windows XP ,am i missing something ,please let me know
Thanks
Naga Aditya
|
|
|
|

|
try changing "LOGON32_LOGON_INTERACTIVE" value to 9
|
|
|
|

|
Hi,
The code was very helpful. The way it was writen also good.
Thanks,
Sri.
Srikanth.Vemulapally
|
|
|
|

|
Very Helpful Code , my vote is 5
Thanks
Ahmed Farag
|
|
|
|

|
Thank you, Ahmed!
Wollen Sie ganz einfach Ihre eigene Homepage erstellen, ohne HTML-Kenntnisse, einfach, professionell und mit viel Freude? Probieren Sie unser Desktop Content Management System (CMS) Zeta Producer für Windows aus. Komplett mit eigenem Shop, Gästebuch, Weblog, Bildergalerien, Integration von YouTube-Videos. Wir haben eine aktive Anwender-Community, schnellen Support, sympathische Support-Mitarbeiter.
|
|
|
|
|

|
How can i call LogonUser with a username "something" and without any password?
|
|
|
|

|
Hello, in your demo project you show how to list system files : is it possible to grant writing files privilege ? I don't know why I can't do this using your code. Any idea ? (I read all the comments in this forum) Thank you. Patrice.
|
|
|
|

|
Ok, I found the problem : we must use LOGON_TYPE = LOGON32_LOGON_INTERACTIVE (and not LOGON32_LOGON_NEW_CREDENTIALS) Note : 'Act as part of operating system' privilege set : only for Windows 2000 users (see MSDN paper) Now it works both on Vista and XP. Patrice.
|
|
|
|

|
I was having all kinds of problems trying to pass client credentials on to IIS which in turn could pass them on to SQL Server. Your class not only worked for me but its implementation and the way to use it in my code is execlent.
Thanks so much.
|
|
|
|

|
Output:
Unknow Error 1346.
using ( new Impersonator( "test-admin", "testclient", "Pa$$word" ) )
{
try
{
using (StreamWriter sw = File.CreateText(@"c:\test_file.txt"))
{
sw.WriteLine("test");
sw.Close();
}
}
catch (Exception err)
{
Console.WriteLine(err.Message.ToString());
}
}
modified on Tuesday, August 23, 2011 5:37 AM
|
|
|
|

|
works a treat! thanks a ton!
|
|
|
|

|
Thanks a lot for your comment, really glad it works with even the latest versions of Windows.
Wollen Sie ganz einfach Ihre eigene Homepage erstellen, ohne HTML-Kenntnisse, einfach, professionell und mit viel Freude? Probieren Sie unser Desktop Content Management System (CMS) Zeta Producer für Windows aus. Komplett mit eigenem Shop, Gästebuch, Weblog, Bildergalerien, Integration von YouTube-Videos. Wir haben eine aktive Anwender-Community, schnellen Support, sympathische Support-Mitarbeiter.
|
|
|
|

|
Hi
when i am using this on my windows server 2008 R2 its giving me the following error
Logon failure: the user has not been granted the requested logon type at this computer
Can you let me know how you fixed it on your machine,
Thanks
Naga Aditya
|
|
|
|

|
It should be updated for more modern versions of Windows
|
|
|
|

|
HI
how to use this code
to use ipersonation to sqlserver from application
besr regards
yacov
|
|
|
|

|
I keep getting an error saying "Unknown Username or Bad Password". I am certain that the credentials I am using are correct. I can connect successfully via windows explorer. Please help!
|
|
|
|

|
Excellent article Uwe keim. It helped me a lot.
Mohammad Vaqqas
|
|
|
|

|
Has anyone Impersonating running on a Windows 7 install?
If yes, how?
regards,
psyeye
|
|
|
|

|
[Please note: The user context that initiates the impersonation (i.e. not the user context to which it is switched to) needs to have the "Act as part of operating system" privilege set.] I have a situation, a normal privileged user is logged in and it wants to impersonate another user (to setup the printer settings under the other user context) - Will your code work? World without walls neither need gates nor windows.
|
|
|
|

|
Genial, simple and 100% working
|
|
|
|

|
Your code really helps me, thank you
|
|
|
|

|
Hi,
Great class! Very useful, thank you for sharing.
I have found that if I use this in unit tests, that when I try to log back into my computer which is part of a domain (when not connected to the domain), my regular user account is no longer cached and I need to reconnect up to the domain to login.
Does anyone know if there is a way to re-cache or not lose the local cache of my regular user account's credentials when using Impersonator?
Thanks,
Jon
|
|
|
|

|
Rather than doing:
using(new Impersonator("user", "domain", "pass"))
{
...
}
You could try:
Impersonator imp = null;
try
{
imp = new Impersonator("user", "domain", "pass");
....
}
catch { }
finally
{
if(imp != null)
{
imp.Dispose();
}
}
|
|
|
|

|
thanks a lot, i still can't understand why AD integration isn't better implemented within .NET
|
|
|
|

|
Thank you very much
Glad I could help you!
|
|
|
|
|

|
Thanks Uwe.
This article and code helped me deliver an application.
I don't know how I would have succeeded without it.
I have (of course) given you credit in my code comments.
Cheers.
|
|
|
|
|

|
hello thanks for this useful topics
|
|
|
|
|

|
Hi,
I tried to use the code on my website. When I use it on my machine it works perfect. But as soon as I deploy it on the server which has SSL, it outputs Logon failure: the user has not been granted the requested logon type at this computer. Is this because I'm using ssl or is it because of server configuration. We have windows server 2003.
I tried more codes also for impersonation actually. I want to impersonate as a LDAP/local user account to perform directory listing of a remote machine. I am not able to get it done. Do you have any Idea about this please?
Can please help? Thanks a lot.
Amit Kumar Thakur.
modified on Monday, December 14, 2009 1:51 AM
|
|
|
|

|
I have used your class alot..
BUT, now I want to do reach a Computer in another domain!!! with a domain account in the second domain, and there is no trust...
I can see that it tries to authenticate me on my own domain, instead of trying the other one!
I'am lost.. any idea!?
modified on Monday, August 24, 2009 9:48 AM
|
|
|
|

|
turbohansen wrote: I can see that it tries to authenticate me on my own domain, instead of trying the other one!
Have you tried including the domain prefix in the username?
ex:
domain\username
|
|
|
|
 |
|
|
General News Suggestion Question Bug Answer Joke Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
|
Introduced a class that simplifies Windows Impersonation in C#.
| Type | Article |
| Licence | CPOL |
| First Posted | 11 Apr 2005 |
| Views | 487,204 |
| Downloads | 16,004 |
| Bookmarked | 187 times |
|
|