943,625 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 2929
  • C# RSS
Jan 10th, 2009
0

want to login to windows through c#

Expand Post »
hello all,
i need some help regarding windows login.i have created an application in C# which replaces the windows-login and which gets username and password from the user to enter the windows.
But my problem is that i have done the user authentication through win32 API function "LogonUser()" and it works fine.but i am not able to enter the windows.it always shows me a black screen.
Please help me what should i do for that is there any library or function or any related material so that i can go ahead.

thanks
DevGeek
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DevGeek is offline Offline
7 posts
since Jan 2009
Jan 10th, 2009
0

Re: want to login to windows through c#

I you don't show us your relevant code I am affraid we can do much to help you out.
Reputation Points: 2023
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,735 posts
since Oct 2008
Jan 10th, 2009
0

Re: want to login to windows through c#

Click to Expand / Collapse  Quote originally posted by ddanbe ...
I you don't show us your relevant code I am affraid we can do much to help you out.
Here is my code but i am not able to enter to my windows desktop.

C# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Text;
  6.  
  7. using System.Runtime.InteropServices; // DllImport
  8. using System.Security.Principal; // WindowsImpersonationContext
  9. using System.Security.Permissions; // PermissionSetAttribute
  10.  
  11. namespace loginwin
  12. {
  13.  
  14. class Program
  15. {
  16.  
  17. enum SECURITY_IMPERSONATION_LEVEL
  18. {
  19. SecurityAnonymous,
  20. SecurityIdentification,
  21. SecurityImpersonation,
  22. SecurityDelegation
  23. }
  24. [DllImport("ADVAPI32.DLL")]
  25. public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
  26. int dwLogonType, int dwLogonProvider, out IntPtr phToken);
  27. [DllImport("ADVAPI32.DLL")]
  28. public extern static bool DuplicateToken(IntPtr ExistingTokenHandle, int
  29. SECURITY_IMPERSONATION_LEVEL, out IntPtr DuplicateTokenHandle);
  30. [DllImport("kernel32.DLL")]
  31. static extern bool CloseHandle(IntPtr hObject);
  32.  
  33.  
  34. public static WindowsImpersonationContext ImpersonateUser(string sUsername, string sDomain, string sPassword)
  35. {
  36. // initialize tokens
  37. IntPtr pExistingTokenHandle = new IntPtr(0);
  38. IntPtr pDuplicateTokenHandle = new IntPtr(0);
  39. pExistingTokenHandle = IntPtr.Zero;
  40. pDuplicateTokenHandle = IntPtr.Zero;
  41.  
  42. // if domain name was blank, assume local machine
  43. if (sDomain == "")
  44. sDomain = System.Environment.MachineName;
  45.  
  46. try
  47. {
  48. string sResult = null;
  49.  
  50. const int LOGON32_PROVIDER_DEFAULT = 0;
  51. // create token
  52. const int LOGON32_LOGON_INTERACTIVE = 2;
  53. //const int SecurityImpersonation = 2;
  54. // get handle to token
  55. bool bImpersonated = LogonUser(sUsername, sDomain, sPassword,LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,out pExistingTokenHandle);
  56.  
  57. // did impersonation fail?
  58. if (false == bImpersonated)
  59. {
  60. int nErrorCode = Marshal.GetLastWin32Error();
  61. sResult = "LogonUser() failed with error code: " +
  62. nErrorCode + "\r\n";
  63.  
  64. // show the reason why LogonUser failed
  65. Console.WriteLine(sResult + "Error");
  66.  
  67. }
  68.  
  69. // Get identity before impersonation
  70. sResult += "Before impersonation: " +
  71. WindowsIdentity.GetCurrent().Name + "\r\n";
  72.  
  73. bool bRetVal = DuplicateToken(pExistingTokenHandle,
  74. (int)SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation,
  75. out pDuplicateTokenHandle);
  76.  
  77. // did DuplicateToken fail?
  78. if (false == bRetVal)
  79. {
  80. int nErrorCode = Marshal.GetLastWin32Error();
  81. // close existing handle
  82. CloseHandle(pExistingTokenHandle);
  83. sResult += "DuplicateToken() failed with error code: "
  84. + nErrorCode + "\r\n";
  85.  
  86. // show the reason why DuplicateToken failed
  87. Console.WriteLine(sResult+ "Error");
  88.  
  89. return null;
  90. }
  91. else
  92. {
  93. // create new identity using new primary token
  94. WindowsIdentity newId = new WindowsIdentity
  95. (pDuplicateTokenHandle);
  96. WindowsImpersonationContext impersonatedUser =
  97. newId.Impersonate();
  98.  
  99. // check the identity after impersonation
  100. sResult += "After impersonation: " +
  101. WindowsIdentity.GetCurrent().Name + "\r\n";
  102.  
  103. Console.WriteLine(sResult+"Success");
  104. return impersonatedUser;
  105. }
  106. }
  107. catch (Exception ex)
  108. {
  109. throw ex;
  110. }
  111. finally
  112. {
  113. // close handle(s)
  114. if (pExistingTokenHandle != IntPtr.Zero)
  115. CloseHandle(pExistingTokenHandle);
  116. if (pDuplicateTokenHandle != IntPtr.Zero)
  117. CloseHandle(pDuplicateTokenHandle);
  118.  
  119.  
  120. }
  121. }
  122. static void Main(string[] args)
  123. {
  124. ImpersonateUser("Username", System.Environment.MachineName.ToString(), "Password");
  125. Console.Read();
  126. }
  127. }
Last edited by DevGeek; Jan 10th, 2009 at 2:12 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DevGeek is offline Offline
7 posts
since Jan 2009
Jan 10th, 2009
0

Re: want to login to windows through c#

Replacing MSGina is not as simple as I tihnk you're looking at there. I would guess a number of those elements fail as you're not logged in at that point. I dont believe its just a case of "impersonating" a specific user, you need windows to go through the real login process - Id imagine your window goes black as you're not calling any method for it to know it needs to launch the shell, and so on.

I wise man said:

Quote ...
You are looking at replacing MS Gina OS level code

This is a VERY NOT TRIVIAL thing, its basically something that custom vendors do. So, best advice? Don't even consider it.
(Taken from
http://www.eggheadcafe.com/community...t-replac.aspx)
Hes right.
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Jan 10th, 2009
0

Re: want to login to windows through c#

Click to Expand / Collapse  Quote originally posted by LizR ...
Replacing MSGina is not as simple as I tihnk you're looking at there. I would guess a number of those elements fail as you're not logged in at that point. I dont believe its just a case of "impersonating" a specific user, you need windows to go through the real login process - Id imagine your window goes black as you're not calling any method for it to know it needs to launch the shell, and so on.

I wise man said:



Hes right.
its too much necessary for me to replace windows login its my final year project.And i have to do it at any cost.So please do suggest some material or any other option(if possible) except Msgina.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DevGeek is offline Offline
7 posts
since Jan 2009
Jan 10th, 2009
0

Re: want to login to windows through c#

And one more thing that i have seen many winlogon theme that just replace the logontheme.exe with logonui.exe in c:\windows\system32\ and they don't change any msgina so please do suggest.what you people say about this.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DevGeek is offline Offline
7 posts
since Jan 2009
Jan 10th, 2009
0

Re: want to login to windows through c#

If its been told to you that you have to do this specific application for your final year, what reading material were you told to look at?
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Jan 11th, 2009
0

Re: want to login to windows through c#

it is my research project no material has been provided therefore i ask here.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DevGeek is offline Offline
7 posts
since Jan 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Load a file to RichTextBox
Next Thread in C# Forum Timeline: Problems with transaction in SQL Server Mobile





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC