0

I "inherited" an ASP.NET Web app where we've been capturing APM data using a Javascript APM client module in the browser. But we discovered that in a production environment, it was recording a very low number of actual users/usernames! I investigated the issue and saw my own browser send the wrong username to the backend.

So,in the code, in each page's Page_Load method, we're looking to see if the Global.appUserName is already set. If not, we then grab the username from HttpContext.Current.Request.LogonUserIdentity.Name. And then we actually do an LDAP lookup to get the person's full name. Finally, we set Global.appUserName to that full name.

The one and only reference to "appUserName" in the Global.asax.cs file is this:

public static string appUserName;

I did some research and found that having this be "static" is probably the root cause of our issue. (Which makes a lot of sense, as I have yet to see this incorrect username reporting issue in DEV, rarely in CERT, but it's very frequent in PROD.)

So, I removed the word "static" and now a dozen different places in the code are complaining "An object reference is required for the non-static field, method, or property 'Global.appUserName'.

I started thinking about how to work around this by still using Global.asax.cs... but I get the feeling that's the wrong strategy? Seems like this is not really an application-wide "global" as much as a variable that we'd like to be accessed easily (identically-named) from every page's Javascript. Here's that Javascript:

<script type="text/javascript">
    window.apmProdUrl = '<%= ConfigurationManager.AppSettings["apmProdUrl"] %>';
    window.apmCertUrl = '<%= ConfigurationManager.AppSettings["apmCertUrl"] %>';
    window.apmTestUrl = '<%= ConfigurationManager.AppSettings["apmTestUrl"] %>';
    window.apmServiceName = '<%= ConfigurationManager.AppSettings["apmServerName"] %>';
    window.appUserName = '<%= global_asax.appUserName %>';
    apmJsAgent("Home Page");
</script>

Can anybody recommend a better strategy for doing this? (I don't have much experience with multi-user web apps. I've mostly dealt with services and a couple of WinForms apps.)

Thanks!

DaveyBoy
  • 435
  • 4
  • 20

1 Answers1

0

Since there were only about 8 pages that actually used this data... I simply removed that global.appUserName property... and instead made a separate public string appUserName property on each of those pages.

I adjusted our Javascript to do.... window.appUserName = '<%= appUserName %>'; on every page, instead of calling global there.

And then I adjusted the code on each page to stop checking for the pre-existence / pre-population of that global property... and instead... just assume it's always blank and go get the username from HTTP context. Word came down that we don't want to see the user's full name anyways in Elastic...just their usernames... so we can eliminate our LDAP call anyways. I'm sure that's why we were checking for its pre-existence and perhaps why the previous developer put that in global.aspx in the first place.

I just pushed this all to DEV and it works. Of course, I won't know for sure until we put it in PROD or maybe beg a half-dozen people to help us test at the same time in CERT.

DaveyBoy
  • 435
  • 4
  • 20