7 Replies - 5709 Views - Last Post: 29 May 2012 - 11:38 AM Rate Topic: -----

#1 BJseal91  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 68
  • Joined: 18-April 09

Use Windows Login Details for Visual Basic Project

Posted 28 May 2012 - 05:36 AM

Hi,
Thanks for taking the interest in this post. I am trying to make a system Login using windows username and password I have got the code for the username already which is

Public Class form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox1.Text = System.Environment.UserName
    End Sub
End Class



What I need is how to confirm the windows password is correct

Example

If John had an account on a windows machine the username will display as John
And then he enters his password the same as he would in windows it then checks Johns permissions and displayers the appropriate form

2 forms

1 Administrator
2 standard users

The code to verify the users rights I have used the following

Public Class form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If My.User.IsInRole(ApplicationServices.BuiltInRole.Administrator) Then
            TextBox2.Text = "Administrator"
        Else
            TextBox2.Text = "Standard"
        End If
    End Sub
End Class



So the only missing peace to the puzzle is confirming the password

Any help is grateful or if you could give me guidance on where to get this information

Kind Regards

Bradley

Is This A Good Question/Topic? 0
  • +

Replies To: Use Windows Login Details for Visual Basic Project

#2 m_wylie85  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 96
  • View blog
  • Posts: 899
  • Joined: 15-October 10

Re: Use Windows Login Details for Visual Basic Project

Posted 28 May 2012 - 05:47 AM

Hey have a look at these three sites they May! help

http://www.dreaminco...min-privileges/

http://www.dotnetspi...ministrato.aspx

http://msdn.microsof...nmentpermission

This post has been edited by m_wylie85: 28 May 2012 - 05:48 AM

Was This Post Helpful? 0
  • +
  • -

#3 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,060
  • Joined: 02-July 08

Re: Use Windows Login Details for Visual Basic Project

Posted 28 May 2012 - 06:11 AM

I don't understand why you need to check the password again, if he logged in he is logged in.
Was This Post Helpful? 0
  • +
  • -

#4 BJseal91  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 68
  • Joined: 18-April 09

Re: Use Windows Login Details for Visual Basic Project

Posted 28 May 2012 - 06:23 AM

View Post_HAWK_, on 28 May 2012 - 07:11 AM, said:

I don't understand why you need to check the password again, if he logged in he is logged in.


Hi Hawk
ok yes that is true they have logged in I am creating this for a charity called Citizens Advice Bureau and what they wanted is the get the user to login to the application with there username and password again to confirm it is them as there is special information for there accounts only on it form them and they where happy to have
it made that they login using there windows id and password

I have the ID part but don't know to confirm the password section of it

Kind Regards

Bradley

View Postm_wylie85, on 28 May 2012 - 06:47 AM, said:



Many thanks I had already seen this way very handy
Was This Post Helpful? 0
  • +
  • -

#5 BJseal91  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 68
  • Joined: 18-April 09

Re: Use Windows Login Details for Visual Basic Project

Posted 29 May 2012 - 01:44 AM

I found this code searching online that is supose to do just that but the code shows just 1 error

Code

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

Private Declare Auto Function CloseHandle Lib "kernel32.dll" ( _
    ByVal hObject As IntPtr) _
As Boolean

Public Enum LogonType As Integer
    LOGON32_LOGON_INTERACTIVE = 2
    LOGON32_LOGON_NETWORK = 3
    LOGON32_LOGON_BATCH = 4
    LOGON32_LOGON_SERVICE = 5
    LOGON32_LOGON_UNLOCK = 7
    LOGON32_LOGON_NETWORK_CLEARTEXT = 8
    LOGON32_LOGON_NEW_CREDENTIALS = 9
End Enum

Public Function IsNTPasswordValid(ByVal Username As String, ByVal Password As String, Optional ByVal Domain As String = "") As Boolean
    Dim Token As New IntPtr
    LogonUser(Username, Domain, Password, LogonType.LOGON32_LOGON_INTERACTIVE, 0, Token)
    CloseHandle(Token)
    If Token.ToInt32 <> 0 Then Return True
End Function


the error states function IsNTPasswordValid don't return a value on all code paths are you missing a "Return" statement

Part to error

Public Function IsNTPasswordValid(ByVal Username As String, ByVal Password As String, Optional ByVal Domain As String = "") As Boolean
            Dim Token As New IntPtr
            LogonUser(Username, Domain, Password, LogonType.LOGON32_LOGON_INTERACTIVE, 0, Token)
            CloseHandle(Token)
            If Token.ToInt32 <> 0 Then Return True
        End Function


any help will be appricated

Kind Regards

Bradley
Was This Post Helpful? 0
  • +
  • -

#6 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Use Windows Login Details for Visual Basic Project

Posted 29 May 2012 - 06:58 AM

That warning is telling you that only if Token.ToInt32 <> 0 is anything returned. You could either put an Else Return False or just put Return False at the end of the function.

Functions should always return something and in this case it's not returning anything if Token.ToInt32 = 0
Was This Post Helpful? 0
  • +
  • -

#7 digitaldevelopment  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 168
  • Joined: 30-April 12

Re: Use Windows Login Details for Visual Basic Project

Posted 29 May 2012 - 11:28 AM

I don't think it'd be smart to post a code that retrieves the user's windows password here, as it could easily be used for... malicious purposes. of course I don't doubt your intentions just the ones of the internet
Was This Post Helpful? 1
  • +
  • -

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,416
  • Joined: 29-May 08

Re: Use Windows Login Details for Visual Basic Project

Posted 29 May 2012 - 11:38 AM

This code suggest to me, you not thinking fully about the logic
  If Token.ToInt32 <> 0 Then Return True



Why not just return the result of the evaluation.
Return Token.ToInt32 <> 0


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1