?
Solved

how to pull active directory login id using vb form

Posted on 2007-10-02
21
Medium Priority
?
363 Views
Last Modified: 2013-11-26
I am using visual studio 2005 to try and create a web form which will pull the active directory id or name from a user when the form loads and place the value in a text box. I am not even sure where to start here. Any help would be appreciated.

thanks,
d-bass
0
Comment
Question by:D-bass
  • 7
  • 7
  • 4
  • +1
19 Comments
 
LVL 12

Expert Comment

by:weareit
ID: 19998529
If you want the currently logged in user you could do this:

Imports System.Windows.Forms
...
...
...
...
TextBox1.Text = System.Windows.Forms.SystemInformation.UserName

-saige-
0
 
LVL 16

Expert Comment

by:SQL_SERVER_DBA
ID: 19998640
Declare Function GetUserName Lib "advapi32.dll" Alias _
       "GetUserNameA" (ByVal lpBuffer As String, _
       ByRef nSize As Integer) As Integer

   Public Function GetUserName() As String
       Dim iReturn As Integer
       Dim userName As String
       userName = New String(CChar(" "), 50)
       iReturn = GetUserName(userName, 50)
       GetUserName = userName.Substring(0, userName.IndexOf(Chr(0)))
   End Function
0
 
LVL 9

Expert Comment

by:JonMny
ID: 20001446


text1.text =Page.User.Identity.Name
0
Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

 

Author Comment

by:D-bass
ID: 20006159
I was able to get the  "TextBox1.Text = Page.User.Identity.Name" to compile but is not returning anything to the textbox1. Do I need to do any kind of conversion?
0
 
LVL 9

Expert Comment

by:JonMny
ID: 20006271
No in my asp.net it returns qa-ent\jonO

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
TextBox1.Text = Page.User.Identity.Name




    End Sub


you could try
        TextBox1.Text = My.User.Name


0
 

Author Comment

by:D-bass
ID: 20006794
Still does not return a value. In my aspx page I am able to return the local account name using the following:

TextBox1.Text = Request.LogonUserIdentity.Name

However, when I publish the page it still pulls the name from the local server. I guess this is an issue with how I have the connection set up?
0
 
LVL 12

Expert Comment

by:weareit
ID: 20006816
You might have to embed some of your code into a page that loads on the client side...  This code does nothing but pulls the local login account, assigns it to a variable and then passes it back to the server as a variable...

-saige-
0
 
LVL 9

Expert Comment

by:JonMny
ID: 20006832
I think you need to make sure it's using impersionation on the server.

<system.web>
        <identity impersonate="true" />
    </system.web>
0
 
LVL 12

Expert Comment

by:weareit
ID: 20007100
I was able to use:

Imports System
Partial Class _Default
      Inherits System.Web.UI.Page
      Protected Sub TextBox1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Load
            TextBox1.Text = My.User.Name
      End Sub
End Class

When I just made a one page project in VS2005.

-saige-
0
 

Author Comment

by:D-bass
ID: 20007344
weareit,

That code does work when i create a "new project" and form. But it does not work when I create a "new website" in VS2005 and create a web form. Do you know the syntax difference between the two?
0
 
LVL 12

Expert Comment

by:weareit
ID: 20007413
In all honesty I would have to say that I do not know of any syntax differences between the two.  My initial response was for a VBAPP.  I'll dig a little depper.

-saige-
0
 
LVL 9

Expert Comment

by:JonMny
ID: 20007498

System.Web.UI.Page.User.Identity.Name
pulls the user ID of the current logged-in user,  whether the user's identity is established with ASP.NET login controls (and implicitly, ASP.NET membership) or with Integrated Windows authentication

I use this all the time in web pages, is it showing aspnet? when you run it on the server


see also
http://msdn2.microsoft.com/en-us/library/ms178344(vs.80).aspx


0
 

Author Comment

by:D-bass
ID: 20186325
I am able to pull the useridentity, but it always pulls the local user i.e adinistrator or Datawhse. Even after publishing the page , when I open the page from my local computer the local user (to the server) is displayed. here is thecode:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.TextBox11.Text = Me.Request.LogonUserIdentity.Name

TextBox11 is always "DATAWHSE\IUSR_DATAWHSE"

Any ideas? Again sorry for lapse of time, priorites changed and I now have some spare time to work on this isssue again.

Thanks!
D-bass
0
 
LVL 9

Accepted Solution

by:
JonMny earned 2000 total points
ID: 20186646
if you put

    Me.TextBox1.Text = Page.User.Identity.Name
        Me.TextBox2.text = Environment.UserName

do they both have DATAWHSE\IUSR_DATAWHSE

0
 

Author Comment

by:D-bass
ID: 20187386
the first line returns nothing
the secound returns "NETWORK SERVICE"
0
 
LVL 9

Expert Comment

by:JonMny
ID: 20187435
are you allowing anonymous requests? If so then  Page.User is blank and Environment.User would be network service.

when Impersonation is enabled ASP.NET will change the impersonation on the underlying thread to match of who's logged on.  For anonymous users this will be the IUSR_ account or if logged on the user who logged on.


0
 

Author Comment

by:D-bass
ID: 20187595
can you tell me how to disable anonymous requests so I can capture the active directory user name?
0
 
LVL 9

Expert Comment

by:JonMny
ID: 20187655
on iis right click on your web site, click on properties, select the directory security tab, click edit under Authentication and access control, uncheck  enable anonymous access
0
 

Author Comment

by:D-bass
ID: 20187719
That was the issue. Worked beautifully. Thank you so much!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Progress
Starting up a Project

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question