-1

I have a webform, and I am trying to make a log of who is using it. All the research I did, was all about using on a local box, not through IIS.

using System.Security.Principal;

private void Form1_Load(object sender, EventArgs e)
        {
            string curuser = WindowsIdentity.GetCurrent().Name;

        }

Where I get the Output:

tw.WriteLine(DateTime.Now + ": " + CurUser);

Output displayed:

12/1/2017 4:13:18 PM: System.Web.UI.WebControls.Label

I am assuming it is something silly and dumb. Thanks guys

Note: I have 'Windows Authentication' enabled within IIS for this site.

David
  • 208,112
  • 36
  • 198
  • 279
dwb
  • 475
  • 6
  • 31
  • I don't see how that code would produce that output. Or any output. – David Dec 01 '17 at 23:10
  • @David updated post with how I am getting the output. – dwb Dec 01 '17 at 23:12
  • What is `tw`? What is `CurUser`? (It's a distinctly different variable than `curuser`.) Where does that new code execute? I suspect you're making some mistakes or false assumptions in your testing, essentially invalidating the question. – David Dec 01 '17 at 23:14
  • @David tw is just a Textwriter, CurUser is the string where I am storing it. The date/time was not in the original post, because I did not include the entire output I was getting, just the relevant piece. Also, I figured out why I was not getting errors, I did not revert back after I was testing. Updating top post, with current information... sorry – dwb Dec 01 '17 at 23:19
  • Please don't change the question such that it invalidates existing answers. This renders the question and answers unusable for future readers. You are certainly invited to add more information to the question, or even to open additional questions. But completely changing the original question after it's been answered is considered vandalizing it. – David Dec 01 '17 at 23:32

1 Answers1

0

Whatever or wherever CurUser is, it's an instance of a Label. It's not your curuser variable that you declared in Page_Load and then never used.

When you try to output an object as a string, the system internally invokes the .ToString() implementation on that object. Unless overridden (and Label doesn't override it), the default implementation for .ToString() (inherited from System.Object) is to output the name of the type. In this case "System.Web.UI.WebControls.Label".

(Makes sense. After all, how would the system inherently know how a complex object, particularly any custom object you could write, should be represented as a string?)

Don't output an unrelated object, output the value:

string curuser = WindowsIdentity.GetCurrent().Name;
tw.WriteLine(DateTime.Now + ": " + curuser);
David
  • 208,112
  • 36
  • 198
  • 279
  • That is exactly what I forgot to correct. I am now getting results that make more sense. The output (now that its correct) is: 12/1/2017 4:21:41 PM: IIS APPPOOL\WebApp - shouldnt that be the Windows Credentials connecting to the app? – dwb Dec 01 '17 at 23:22
  • @dwb: I'm not 100% certain without testing it, but I *suspect* that what you're grabbing is the user context of the application itself and not the connected web user. Try this instead: `string curuser = User.Identity.Name;` (`User` is a property on the `Page` object.) – David Dec 01 '17 at 23:24
  • That one just shows a blank response. 12/1/2017 4:32:11 PM:- Also, I forgot to mention, the users are not logging into the site, not sure if maybe thats the issue, and I am going the wrong direction. – dwb Dec 01 '17 at 23:33
  • @dwb: This also looks potentially helpful: https://stackoverflow.com/questions/19676312/how-to-get-user-name-using-windows-authentication-in-asp-net – David Dec 01 '17 at 23:35
  • That looks like exactly what I need. Thanks for the direction. – dwb Dec 01 '17 at 23:38