Hi, trying to get the username of the user logged into a computer but it doesn't appear to be working. I was using Label1.Text = User.Identity.Name; to check if it did work but it wasn't displaying anything. The only reason I need the username is to send it through to an SQL stored procedure to keep track of who is downloading reports that are being generated, this system will be an internal system for a company. Any help with this would be great, thank you.

Dani AI

Generated

For an internal reporting site the usual goal is to record the Windows account that requested a download. As showed, a direct read of the request identity can appear empty unless the site and client are configured to provide credentials. is correct that the identity isn't persisted automatically, and is pointing to the core requirement: the server must accept Windows credentials.

Two practical approaches:

  • Simple and robust (recommended): accept Windows credentials at the web tier, read the request principal on the server, and pass that value explicitly into the stored procedure as a parameter. This avoids Active Directory changes and the Kerberos “double hop.” Browsers must be allowed to send integrated credentials (intranet zone / browser policy). Example SQL call pattern:
using (var cmd = new SqlCommand("usp_LogDownload", conn))
{
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@DownloadedBy", username ?? string.Empty);
    // other params...
    cmd.ExecuteNonQuery();
}
  • Full Windows delegation (advanced): enable impersonation and configure Kerberos/SPNs so SQL Server sees the original user automatically. This requires AD admin work (register SPNs for the web service account, enable constrained delegation) and careful app-pool/service-account choices. Example web.config fragment:
<system.web>
  <authentication mode="Windows" />
  <identity impersonate="true" />
</system.web>

Troubleshooting notes and cautions: confirm anonymous auth is off and Windows auth is on in IIS; verify the browser actually sends credentials; check the incoming principal format (DOMAIN\user); if delegation is attempted, verify SPNs and delegation settings in AD. For most intranet reporting scenarios, capturing the server-side principal and passing it into the stored procedure is the simplest, safest solution.

Recommended Answers

All 3 Replies

Are you storing the logged in user's user name anywhere? In a session variable for instance?
Withoutknowing what you are doing to persist the user name after a successful login there isn't much advice we can give.
For example, User.Identity.Name doesn't get populated with a value without you making it so.

The user doesn't have to log in to the system. I'm trying to get the username they use to log on to the computer. Not sure if this can be done or not.

For you to get that information using User.Identity.Name, the website needs to be configured (in IIS) by enabling Windows Authentication and disabling anonymous authentication. With that action, you will get the logged on userid.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.