Results 1 to 19 of 19

Thread: [02/03] Determining XP Login Credentials within VB

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    115

    [02/03] Determining XP Login Credentials within VB

    Is there a simple way to determine if the user running a vb program logged in to windows with administrator rights?

    The stuff I found on msdn seems very complex.

    Update:

    All I really care about is the user name. It does not have to be high security.
    I found this function however I dont think it works in 2002. Anyone know an equivalent?

    Code:
    Function GetUserName() As String
            If TypeOf My.User.CurrentPrincipal Is _
            Security.Principal.WindowsPrincipal Then
                ' The application is using Windows authentication.
                ' The name format is DOMAIN\USERNAME.
                Dim parts() As String = Split(My.User.Name, "\")
                Dim username As String = parts(1)
                Return username
            Else
                ' The application is using custom authentication.
                Return My.User.Name
            End If
        End Function
    Last edited by jeffnyc; Jul 11th, 2007 at 10:32 PM.

  2. #2
    Addicted Member Gonegaming12's Avatar
    Join Date
    Jun 2006
    Location
    USA
    Posts
    131

    Re: [02/03] Determining XP Login Credentials within VB

    ill make it easy on you, try this.

    Code:
      
    my.User.IsInRole(ApplicationServices.BuiltInRole.Administrator)
    server different roles in there if you want to mess with those.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    115

    Re: [02/03] Determining XP Login Credentials within VB

    Quote Originally Posted by Gonegaming12
    ill make it easy on you, try this.

    Code:
      
    my.User.IsInRole(ApplicationServices.BuiltInRole.Administrator)
    server different roles in there if you want to mess with those.
    Does it work in 02?
    Last edited by jeffnyc; Jul 11th, 2007 at 10:42 PM.

  4. #4
    Addicted Member Gonegaming12's Avatar
    Join Date
    Jun 2006
    Location
    USA
    Posts
    131

    Re: [02/03] Determining XP Login Credentials within VB

    i have 2005, thats why i said to try it, I think its there but not 100%.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    115

    Re: [02/03] Determining XP Login Credentials within VB

    Unfortunately, I dont think this works in 02/03....

  6. #6
    Addicted Member Gonegaming12's Avatar
    Join Date
    Jun 2006
    Location
    USA
    Posts
    131

    Re: [02/03] Determining XP Login Credentials within VB

    that doesn't work, ill see if i can find something else for ya.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [02/03] Determining XP Login Credentials within VB

    The entire My namespace is new in VB 2005.

    The IsInRole method ultimately calls the CheckTokenMembership API function. You could do that yourself, although I make no representations about exactly how to determine the values for its TokenHandle and SidToCheck arguments. I used .NET Reflector to determine how IsInRole was implemented. You could do the same and try to implement the something similar. It's fairly convoluted stuff though.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    Addicted Member Gonegaming12's Avatar
    Join Date
    Jun 2006
    Location
    USA
    Posts
    131

    Re: [02/03] Determining XP Login Credentials within VB

    Wow i didn't know 2005 was the only one that had the my namespace. I mean seriously isn't that everyones favorite namespace? I think a poll should be started on this...

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [02/03] Determining XP Login Credentials within VB

    You might also be able to use WMI. I'm not saying it would be easier or harder and I don't know what class(es) you'd use but I'm sure it would be possible because WMI knows almost everything about your system.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    115

    Re: [02/03] Determining XP Login Credentials within VB

    Quote Originally Posted by jmcilhinney
    The entire My namespace is new in VB 2005.

    The IsInRole method ultimately calls the CheckTokenMembership API function. You could do that yourself, although I make no representations about exactly how to determine the values for its TokenHandle and SidToCheck arguments. I used .NET Reflector to determine how IsInRole was implemented. You could do the same and try to implement the something similar. It's fairly convoluted stuff though.

    Yeah, I'm kind of hoping for something simple....

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [02/03] Determining XP Login Credentials within VB

    The MSDN documentation for the CheckTokenMembership function has a C++ code example for determining whether the current user is an administrator. It would certainly depend on your experience level, but reimplementing that in VB.NET would not be too difficult. The second link contains VB6 code and information that would prove useful.

    http://msdn2.microsoft.com/en-us/library/aa376389.aspx
    http://support.microsoft.com/default.aspx/kb/288900/
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    115

    Re: [02/03] Determining XP Login Credentials within VB

    I googled CheckTokenMembership and VB and found this:

    Code:
    System.Security.Principal.WindowsIdentity.GetCurrent.Name
    and this is good enough for my purposes...

    Code:
     
    Function GetUserName() As String
            Dim tempString As String
            tempString = System.Security.Principal.WindowsIdentity.GetCurrent.Name
            Dim parts() As String = Split(tempString, "\")
            Dim username As String = parts(1)
            Return username
    End Function
    Last edited by jeffnyc; Jul 12th, 2007 at 12:40 AM.

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [02/03] Determining XP Login Credentials within VB

    If all you want is the usre name then you can just use the Environment.UserName property. I thought the point was to determine whether that user was an administrator or not.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    115

    Re: [02/03] Determining XP Login Credentials within VB

    I'm not sure if I can get away with just checking the username - have to ask the client..... But I ended up installing 2005 version anyway =)

    Code:
    MessageBox.Show(My.User.IsInRole(ApplicationServices.BuiltInRole.Administrator.ToString))
    is returning False. But when I go to Control Panel, User Accounts, My account type is Computer Administrator - I thought it would show False for Limited or Guest Accounts and True for Computer Administrator ?

    Update:t - I change my code to

    Code:
     If My.User.IsInRole(ApplicationServices.BuiltInRole.Administrator) Then
                Debug.WriteLine("User is an Administrator")
            Else
                Debug.WriteLine("User is not an Administrator")
            End If
    and it now says I am an administrator. I must be missing something For some reason, using the "toString" causes it to return a false???
    Last edited by jeffnyc; Jul 15th, 2007 at 02:36 PM.

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [02/03] Determining XP Login Credentials within VB

    If you have a value that is not a string and then you call its ToString method then you end up with a completely different object. Is it a surprise that passing two completely different objects gives two different results?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    115

    Re: [02/03] Determining XP Login Credentials within VB

    I would just think True.toString would give you True.

  17. #17
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [02/03] Determining XP Login Credentials within VB

    No, True.ToString gives you "True". True is a Boolean, stored in memory as a 32-bit integer with each bit set to 1. "True" is a String, stored in memory as four 64-bit values containing the Unicode values of the characters 'T', 'r', 'u' and 'e'. Obviously quite different. Is a dog the same thing as the word "dog"? One is a living, breathing animal while the other is three letters.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    115

    Re: [02/03] Determining XP Login Credentials within VB

    Ok Is it afternoon there? Its almost 4am here so I'm trying to follow you... while we are on the subject of booleans and dogs, . if i pass an integer to a procedure that takes a string will it convert it correctly (i.e. delay as integer = 1000) will it automatically convert to "1000" inside the procedure

    I am assuming the answer is yes but I hate to assume.... and I apologize for going off the subject of this thread....

  19. #19
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [02/03] Determining XP Login Credentials within VB

    The answer is that you should NEVER pass an object of one type if an object of another type is expected. With Option Strict turned Off, many conversions will be done for you implicitly, and the one you mention is one of them. That said, you should always have option strict turned On and make all your conversions explicitly. That will help you learn quicker and avoid many errors.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width