• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1542
  • Last Modified:

WinForms: Windows Authentication

Hello, I'm creating a few winform applications that will integrate with my company's webform intranet.  The webforms use Windows authentication, so employees only have to use their IIS login.  I wish to do the same for the winforms but I haven't been able to find anything on the internet explaining how to do this.  Yes, it is a little redundant since the user is already logged into the machine, but I just want to verify that it is that user running the application.  So is there any way to use windows authentication in a winform?
Thanks,
Christion
0
aplogic
Asked:
aplogic
  • 4
  • 3
1 Solution
 
aplogicAuthor Commented:
Or is there (after asking for their username and password) a windows authentication function that takes the username and password and returns a boolean if the user authenticates?
0
 
EBatistaCommented:
try using the WindowsIdentity class

Dim theIdentity As WindowsIdentity = WindowsIdentity.GetCurrent()
Dim thePrincipal As New WindowsPrincipal(theIdentity)

If  thePrincipal.Identity.IsAuthenticated Then
  <tasks>        
End if

you have to import the namespace Principal:
Imports System.Security.Principal
0
 
aplogicAuthor Commented:
Hmm... that checks to see if the person is logged into the computer (and you can also then check roles, etc).  
But I want to verify that the person opening my program is the same person logged into the computer.  So I want to ask them their windows username and password again (basically like windows authentication for web forms).
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
EBatistaCommented:
then u can pull the user name from WindowsPrincipal and compare with the user input:

If  thePrincipal.Identity.Name=TextBox1.Tex Then
  <tasks>
End if
0
 
aplogicAuthor Commented:
What about the password- how do I verify that they put in their correct Windows password?
0
 
EBatistaCommented:
u'r right, then u need to use the unmanaged LogonUser function, you must declare it in the declaration seccion of your class:
Public Class Form1
    Inherits System.Windows.Forms.Form

Private Declare Auto Function LogonUser Lib "advapi32.dll" (ByVal lpszUsername As [String], _
ByVal lpszDomain As [String], ByVal lpszPassword As [String], _
ByVal dwLogonType As Integer, ByVal dwLogonProvider As Integer, _
ByRef phToken As IntPtr) As Boolean


'checking user and password
Private Sub btnCheckUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheckUser.Click

Dim tokenHandle As New IntPtr(0)
tokenHandle = IntPtr.Zero

Dim pws=TextBoxPWS.Text
Dim user=TextBoxUserName.Text
Dim domain as string=TextBox_MachineOrDomainToWhichLogOn.Text

Dim logonResult As Boolean = LogonUser(user,domain, pws, 2, 0, tokenHandle)

If logonResult Then
              MessageBox.Show("OK")
        Else
            MessageBox.Show("Fail")
End If

End Sub



regards
0
 
aplogicAuthor Commented:
That was exactly what I was looking for.  
Thanks!
Christion
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now