want to login to windows through c#

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2009
Posts: 7
Reputation: DevGeek is an unknown quantity at this point 
Solved Threads: 0
DevGeek's Avatar
DevGeek DevGeek is offline Offline
Newbie Poster

want to login to windows through c#

 
0
  #1
Jan 10th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,958
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 284
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: want to login to windows through c#

 
0
  #2
Jan 10th, 2009
I you don't show us your relevant code I am affraid we can do much to help you out.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 7
Reputation: DevGeek is an unknown quantity at this point 
Solved Threads: 0
DevGeek's Avatar
DevGeek DevGeek is offline Offline
Newbie Poster

Re: want to login to windows through c#

 
0
  #3
Jan 10th, 2009
Originally Posted by ddanbe View Post
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.

  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: want to login to windows through c#

 
0
  #4
Jan 10th, 2009
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:

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.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 7
Reputation: DevGeek is an unknown quantity at this point 
Solved Threads: 0
DevGeek's Avatar
DevGeek DevGeek is offline Offline
Newbie Poster

Re: want to login to windows through c#

 
0
  #5
Jan 10th, 2009
Originally Posted by LizR View Post
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.
Regards,
DevGeek
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 7
Reputation: DevGeek is an unknown quantity at this point 
Solved Threads: 0
DevGeek's Avatar
DevGeek DevGeek is offline Offline
Newbie Poster

Re: want to login to windows through c#

 
0
  #6
Jan 10th, 2009
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.
Regards,
DevGeek
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: want to login to windows through c#

 
0
  #7
Jan 10th, 2009
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?
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 7
Reputation: DevGeek is an unknown quantity at this point 
Solved Threads: 0
DevGeek's Avatar
DevGeek DevGeek is offline Offline
Newbie Poster

Re: want to login to windows through c#

 
0
  #8
Jan 11th, 2009
it is my research project no material has been provided therefore i ask here.
Regards,
DevGeek
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC