Using Windows Authentication in C#


In this post I will be showing a simple ways to use windows login in your C# application
   
            Integrated Windows Authentication (IWA) is a term associated with Microsoft products that refers to the SPNEGO, Kerberos, and NTLMSSP authentication protocols with respect to SSPI functionality introduced with Microsoft Windows 2000 and included with later Windows NT-based operating systems. The term is used more commonly for the automatically authenticated connections between Microsoft Internet Information Services, Internet Explorer, and other Active Directory aware applications.

IWA is also known by several names like HTTP Negotiate authentication, NT Authentication,NTLM Authentication, Domain authentication, Windows Integrated Authentication, Windows NT Challenge/Response authentication, or simply Windows Authentication. (From Wikipedia)

To use the Windows Authentication in your application you should include the <System.Runtime.InteropServices> Namespace using the code above

Using :
 using System.Runtime.InteropServices;  

Globale declarations :
 [DllImport("advapi32.dll")]  
 public static extern bool LogonUser(string name, string domain, string pass, int logType, int logpv, ref IntPtr pht);  

Button click event
 IntPtr th = IntPtr.Zero;  
       bool log = LogonUser(txt_name.Text, "workgroup", txt_pass.Text, 2, 0, ref th);  
       if (log)  
       {  
         MessageBox.Show("Good!!");  
       }  
       else  
       {  
         MessageBox.Show("err!!");  
       }  




Download