Solved

Code not pulling user name from computer

Posted on 2009-05-04
10
377 Views
Last Modified: 2012-05-06
I am currently modifying one of my application to log the username from the computer.  Well, something in my code must be off because when I debug, it won't pull the user name.  Can someone please help me. Here's the string I use on my other applications but for some reason it's not working on this one.
strUserName = Mid(User.Identity.Name.ToString(), 11)

Open in new window

0
Comment
Question by:dlavar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
10 Comments
 
LVL 13

Expert Comment

by:usachrisk1983
ID: 24297997
Is this an ASP application?  If so, you'll need to turn off Anonymous Access on the IIS site in order to capture the username.  With this enabled, the user isn't prompted to login (nor are credentials automatically passed if using IE with this option turned on).
0
 
LVL 15

Expert Comment

by:oobayly
ID: 24298057
Your code is assuming that the hostname is 10 chars long, also User.Identity.Name is already a string, so no need for the ToString() method.

I'd suggest the following:
' User.Identity.Name = "Host\User"
Dim name As String = User.Identity.Name.Split("\"c)(1)

Open in new window

0
 

Author Comment

by:dlavar
ID: 24298062
Yes it is an ASP Application and how do you turn off the Anonymous Access off?
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 13

Expert Comment

by:usachrisk1983
ID: 24298121
What version of IIS are you using?

Here are the instructions for IIS6:
http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/524404dc-8586-46b0-89ac-0f5db6d33c9c.mspx?mfr=true

Under IIS 5, you hit the properties of the site and then go to the authentication tab (or maybe directory security, I don't recall the exact name) and then they'll be a button or checkbox somewhere on that dialog to uncheck anonymous access.

When you turn this off, you're forcing users to login to the application, you'll need to enable Windows Authentication (or whatever mechanism you have in place) and users who are not setup to properly pass their credentials through to the webserver (users not on your network or who don't have your site in their trusted list) will be prompted to login.
0
 

Author Comment

by:dlavar
ID: 24298197
What if I don't want them to log on and just pull the user name?  Is that possible?
0
 
LVL 13

Expert Comment

by:usachrisk1983
ID: 24298250
If anonymous access is turned on, then IIS wont send a 401 in the headers of your request, which means the credentials wouldn't be sent from client to server, so you wouldn't have them.  If you only needed this on one or two pages, you could probably send the 401 in the header on that page, but if the user didn't have the option "Automatic Logon with Current Username and Password" setup in IE for the zone your site is in, they will be prompted.

Here's a ColdFusion example of how to send the header,
http://www.technicallychris.com/2006/09/12/getting-the-ntlm-info-with-anonymous-access-on/

... you'd need to modify (or have a more senior ASP person modify) it for .NET.  

To answer your question more directly, if you don't want them to log on, then they wont be logging on, but that means that there is no username for your to grab.
0
 
LVL 13

Expert Comment

by:usachrisk1983
ID: 24329058
I would like to know what your solution was (as I'm sure other EE members would) and have points assigned appropriately if the comments provided by myself or oobayly got you to your solution.
0
 

Accepted Solution

by:
dlavar earned 0 total points
ID: 24329184
For my solution, I went to the web configuration page and changed the following code.  Instead of allowing all users I closed that section and opened the deny users option.  When I did that I was able to pull the username although I'm not requiring them to log in.
<authorization>
			<!--allow users="*"/-->
			<!-- Allow all users -->
			<deny users="?"/>
			<!--  <allow     users="[comma separated list of users]"
                             roles="[comma separated list of roles]"/>
                  <deny      users="[comma separated list of users]"
                             roles="[comma separated list of roles]"/>
            -->
		</authorization>

Open in new window

0
 
LVL 13

Expert Comment

by:usachrisk1983
ID: 24329280
Awesome, I'm sure that will help someone out in the future :)
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

751 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