[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
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.
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?
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%.
Re: [02/03] Determining XP Login Credentials within VB
Unfortunately, I dont think this works in 02/03....
Re: [02/03] Determining XP Login Credentials within VB
that doesn't work, ill see if i can find something else for ya.
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.
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...
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.
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....
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/
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
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.
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 :confused: For some reason, using the "toString" causes it to return a false???
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?
Re: [02/03] Determining XP Login Credentials within VB
I would just think True.toString would give you True.
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.
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....
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.