Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

I'm managed to find some C# code, but I'd like some help converting it to VB.NET.

 

And heres what I have so far in the VB.NET. It compiles but doesn't run! Why not? It gives me the error message "Specified cast is not valid." on the first line (dim p as Windows...etc)

 

This is the VB.NET converted code

Dim p As WindowsPrincipal = Thread.CurrentPrincipal
txtOutput.Text = GetFullName(p.Identity.Name)

 

This is the C# code

WindowsPrincipal p = Thread.CurrentPrincipal as WindowsPrincipal;
txtOutput.Text = GetFullName(p.Identity.Name);

 

And heres the entire VB.NET project (except for the windows generated stuff)

   Private Sub btnCurrentUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCurrentUser.Click
       Dim p As WindowsPrincipal = Thread.CurrentPrincipal
       txtOutput.Text = GetFullName(p.Identity.Name)
       'WindowsPrincipal p = Thread.CurrentPrincipal as WindowsPrincipal;
       'Response.Write(GetFullName(p.Identity.Name));
   End Sub

   Private Function GetFullName(ByVal strLogin As String) As String

       Dim str As String = ""

       'Parse the string to check if domain name is present.
       Dim idx As Integer = strLogin.IndexOf("\\")
       If idx = -1 Then
           idx = strLogin.IndexOf("@")
       End If

       Dim strDomain As String
       Dim strName As String

       If idx <> -1 Then
           strDomain = strLogin.Substring(0, idx)
           strName = strLogin.Substring(idx + 1)
       Else
           strDomain = Environment.MachineName
           strName = strLogin
       End If

       'TODO: Not sure about this conversion
       'DirectoryEntry obDirEntry = null;
       Dim obDirEntry As DirectoryEntry

       Try
           obDirEntry = New DirectoryEntry("WinNT://" + strDomain + "/" + strName)
           Dim coll As System.DirectoryServices.PropertyCollection = obDirEntry.Properties
           Dim obVal As Object = coll("FullName").Value
           str = obVal.ToString()
       Catch ex As Exception
           str = ""
           Trace.Write(ex.Message)
       End Try

       GetFullName = str

   End Function

 

Note that I am also importing these things at the top:

Imports System.DirectoryServices
Imports System.Security.Principal
Imports System.Collections.Specialized
Imports System.Threading

 

Thanks

 

Sam

Edited by samsmithnz
Posted
I was trying things long that line too, like CType(), but neither Ctype or DirectCast work. They both return the error message "Specified cast is not valid", as does not using any of those functions at all. I get why its happening, they are different types, but why does it work in c# but not vb.net?
Posted
that gives you the currently logged on user:-\ which i understood you were looking for, but i guess you've got it going a different way :)

 

Oh, right... this looks like a much quicker way though... but I'll still have to query ActiveDirectory, as I have to find the current user, their logon name, Full name and whether or not they belong to a certain admin group...

  • Leaders
Posted

well i'm not sure how asp.net would do it, but if you want a bit more info on Security.Principle.WindowsIdentity , i knocked this up to show an example...

       Dim user As Security.Principal.WindowsIdentity = Security.Principal.WindowsIdentity.GetCurrent()
       Dim strInfo As String
       strInfo += user.Name & Environment.NewLine
       strInfo += user.AuthenticationType & Environment.NewLine
       strInfo += user.Token.ToInt32 & Environment.NewLine
       If user.IsAuthenticated Then
           MessageBox.Show(strInfo & " is logged on with an authenticated account")
       ElseIf user.IsGuest Then
           MessageBox.Show(strInfo & " is logged on as a guest")
       ElseIf user.IsAnonymous Then
           MessageBox.Show(strInfo & " is anonymous")
       End If

Posted
Is there any reason that the Security.Principal.WindowsIdentity.GetCurrent() would not work with windows 98? It appears to work just fine when I debug and run in XP, but running my app on a user's machine with 98, it does not obtain the logged in user's name. Does anyone know of another way to get the logged in user's name in 98?
Posted

Still stuck.

 

I found a good example for logging on and finding all the groups the user belongs too, but all I need now is an object that will return the current user (not the server user), that I can use in this ASP.NET to show items that belong to them.

 

Someone must have donw this before...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...