|
-
Jul 11th, 2007, 10:20 PM
#1
Thread Starter
Lively Member
[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.
-
Jul 11th, 2007, 10:37 PM
#2
Addicted Member
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.
-
Jul 11th, 2007, 10:38 PM
#3
Thread Starter
Lively Member
Re: [02/03] Determining XP Login Credentials within VB
 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.
-
Jul 11th, 2007, 10:41 PM
#4
Addicted Member
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%.
-
Jul 11th, 2007, 10:42 PM
#5
Thread Starter
Lively Member
Re: [02/03] Determining XP Login Credentials within VB
Unfortunately, I dont think this works in 02/03....
-
Jul 11th, 2007, 10:45 PM
#6
Addicted Member
Re: [02/03] Determining XP Login Credentials within VB
that doesn't work, ill see if i can find something else for ya.
-
Jul 11th, 2007, 10:50 PM
#7
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.
-
Jul 11th, 2007, 10:54 PM
#8
Addicted Member
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...
-
Jul 11th, 2007, 10:57 PM
#9
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.
-
Jul 11th, 2007, 11:03 PM
#10
Thread Starter
Lively Member
Re: [02/03] Determining XP Login Credentials within VB
 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....
-
Jul 11th, 2007, 11:19 PM
#11
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/
-
Jul 12th, 2007, 12:36 AM
#12
Thread Starter
Lively Member
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.
-
Jul 12th, 2007, 02:08 AM
#13
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.
-
Jul 15th, 2007, 02:23 PM
#14
Thread Starter
Lively Member
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.
-
Jul 16th, 2007, 01:56 AM
#15
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?
-
Jul 16th, 2007, 02:29 AM
#16
Thread Starter
Lively Member
Re: [02/03] Determining XP Login Credentials within VB
I would just think True.toString would give you True.
-
Jul 16th, 2007, 02:45 AM
#17
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.
-
Jul 16th, 2007, 02:56 AM
#18
Thread Starter
Lively Member
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....
-
Jul 16th, 2007, 08:23 PM
#19
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|