how do you get current username in aspx?

F

Flip

I have tried everything I can think of, but nothing is working for me. Just
when I read an example and I'm 95% of the way done, I can't actually update
the xml file. :<
(http://www.devhood.com/Tutorials/tutorial_details.aspx?tutorial_id=96)
Very disappointing as the reason I came to aspx was for it's supposed
simplicity to JSPs. :< I hope I wasn't wrong.

I've tried the following and all I get is this.

Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.


These are the lines I've tried:

testing.Text = Thread.CurrentPrincipal.ToString();



testing.Text = User.ToString();



testing.Text =
System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();



The aspx compiles so I'm assuming I have all the right imports, what could I
be doing wrong? The reason why I'm on this ride is I can't actually update
the XML file, if you know how I can do that it would be greatly appreciated!
:>
 
F

Flip

be doing wrong? The reason why I'm on this ride is I can't actually
I think I got it to work, but in a very weird way. I had to give Modify
access to the Users account, but I don't understand. A few websites I read
said to add write privileges to the ASPNet user and other say to the
IUSR_machinename user. What's with all the different solutions?

Any thoughts to help clear up my foggy mind would greatly be apprectiated.
Thank you.
 
M

Matt Berther

Hello Flip,

Given your examples below, I am assuming youre using Windows Authentication.

It should be as simple as: HttpContext.Current.User.Name
 
P

Paul Clement

¤ > be doing wrong? The reason why I'm on this ride is I can't actually
¤ > update
¤ I think I got it to work, but in a very weird way. I had to give Modify
¤ access to the Users account, but I don't understand. A few websites I read
¤ said to add write privileges to the ASPNet user and other say to the
¤ IUSR_machinename user. What's with all the different solutions?
¤
¤ Any thoughts to help clear up my foggy mind would greatly be apprectiated.
¤ Thank you.
¤

The solution would depend upon the type of authentication that is implemented and whether
impersonation is being used. The following should help explain the different scenarios:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/secnetap05.asp


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
F

Flip

Given your examples below, I am assuming youre using Windows
Authentication.
Yup, I was trying the forms authentication but having some problems. So
before I work out those bugs, I thougt I would use the Windows
authentication to get the app up and running first.
It should be as simple as: HttpContext.Current.User.Name
Thank you, I'll try that again tonight! :> Any ideas on why those other
ones wouldn't work? I got them from the web and thought they would be easy
enough to get to work.

Thanks.
 
M

Matt Berther

Hello Flip,
testing.Text = Thread.CurrentPrincipal.ToString();

This wont work because 1) You probably want Thread.CurrentPrincipal.Identity.Name
and 2) this would be the IPrincipal for the executing thread. In all likelihood,
this will be your ASPNET account.
testing.Text = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();

This code is correct, but is still subject to item 2 from the previous item.
testing.Text = User.ToString();

This is closest to my suggestion, however like in item 1, I believe you want
User.Identity.Name.

Also, I just realized my code should have been HttpContext.Current.User.Identity.Name.
Sorry for any confusion.
 
F

Flip

testing.Text =
This code is correct, but is still subject to item 2 from the previous
item.
If this is correct, can you see any reason why I would be getting a NPE for
the GetCurrent()? I think that's the null I'm seeing. :<
This is closest to my suggestion, however like in item 1, I believe you
want
I was just trying to find out what user/group is being used to execute the
script at runtime to find out the user/groups I need to give permission to
write to a file. That's what started this all off. Oddly I had to give
write access to Users, not the IUSR_machinename or the ASPNET account. :<
Also, I just realized my code should have been
HttpContext.Current.User.Identity.Name. Sorry for any confusion.
No probs, I haven't tried it yet, it'll be tonight. Thank you for the
clarification! :>
 
F

Flip

Howdy. I tried the following lines and to my astonishment, nothing is
printed in the Trace for the username. What is happening here?

Trace.Write("username='" + HttpContext.Current.User.Identity.Name.ToString()
+"'");

Trace.Write("usertostring=" + HttpContext.Current.User.ToString());

Trace.Write("type=" + HttpContext.Current.User.GetType().FullName);



THis gives this in the trace output.

username='' 0.003853 0.003408
usertostring=System.Security.Principal.WindowsPrincipal 0.004247
0.000394
type=System.Security.Principal.WindowsPrincipal
 
P

Paul Clement

¤ Howdy. I tried the following lines and to my astonishment, nothing is
¤ printed in the Trace for the username. What is happening here?
¤
¤ Trace.Write("username='" + HttpContext.Current.User.Identity.Name.ToString()
¤ +"'");
¤
¤ Trace.Write("usertostring=" + HttpContext.Current.User.ToString());
¤
¤ Trace.Write("type=" + HttpContext.Current.User.GetType().FullName);
¤
¤
¤
¤ THis gives this in the trace output.
¤
¤ username='' 0.003853 0.003408
¤ usertostring=System.Security.Principal.WindowsPrincipal 0.004247
¤ 0.000394
¤ type=System.Security.Principal.WindowsPrincipal
¤
¤

That doesn't look like valid syntax to me. Are you trying to retrieve the first and last name of the
user?


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
F

Flip

That doesn't look like valid syntax to me. Are you trying to retrieve the
first and last name of the
user?
Howdy. I'm just trying to see something more than a null actually. Not
trying to be flippant, sorry, I just expected to see some text that made
sense to me, something like ASPNET or IUSR_machinename, annonymou or even my
own name, but nothing comes out and when I query the object I get nulls. :<
If it's the wrong syntax, I would have hoped Visual Studio would have given
me a compile error. :< I'm not sure what I'm doing wrong?

OH, the numbers coming up in that cut'n'paste is the cut'n'paste from the
Trace window in html that's outputted with the page, and I put the single
quotes to try to figure out if there was something, anything there.
 
P

Paul Clement

¤ > That doesn't look like valid syntax to me. Are you trying to retrieve the
¤ > first and last name of the
¤ > user?
¤ Howdy. I'm just trying to see something more than a null actually. Not
¤ trying to be flippant, sorry, I just expected to see some text that made
¤ sense to me, something like ASPNET or IUSR_machinename, annonymou or even my
¤ own name, but nothing comes out and when I query the object I get nulls. :<
¤ If it's the wrong syntax, I would have hoped Visual Studio would have given
¤ me a compile error. :< I'm not sure what I'm doing wrong?
¤
¤ OH, the numbers coming up in that cut'n'paste is the cut'n'paste from the
¤ Trace window in html that's outputted with the page, and I put the single
¤ quotes to try to figure out if there was something, anything there.
¤

Well, that doesn't help me understand what you're after. ;-)

The first Trace statement is valid and should return the domain and user ID. The second and third
statements are syntactically invalid. Perhaps this isn't immediately reflected because you're using
C#.


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top