Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / January 2005

how do you get current username in aspx?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Flip - 26 Jan 2005 03:29 GMT
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!
Flip - 26 Jan 2005 04:05 GMT
> 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.
Paul Clement - 26 Jan 2005 14:52 GMT
¤ > 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/s
ecnetap05.asp


Paul ~~~ pclement@ameritech.net
Microsoft MVP (Visual Basic)
Flip - 26 Jan 2005 15:15 GMT
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/s
ecnetap05.asp
Thank you Paul for the link, I'm reading it now! :>
Matt Berther - 26 Jan 2005 07:59 GMT
Hello Flip,

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

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

--
Matt Berther
http://www.mattberther.com

> 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
[quoted text clipped - 22 lines]
> actually update the XML file, if you know how I can do that it would
> be greatly appreciated! :>
Flip - 26 Jan 2005 15:11 GMT
> 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.
Matt Berther - 26 Jan 2005 17:43 GMT
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.

--
Matt Berther
http://www.mattberther.com

>> Given your examples below, I am assuming youre using Windows
>> Authentication.
[quoted text clipped - 10 lines]
>
> Thanks.
Flip - 26 Jan 2005 18:09 GMT
>> testing.Text =
>> System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
> 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! :>
Flip - 29 Jan 2005 18:34 GMT
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
Paul Clement - 31 Jan 2005 14:34 GMT
¤ 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 ~~~ pclement@ameritech.net
Microsoft MVP (Visual Basic)
Flip - 31 Jan 2005 15:32 GMT
> 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.
Paul Clement - 31 Jan 2005 18:01 GMT
¤ > 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 ~~~ pclement@ameritech.net
Microsoft MVP (Visual Basic)

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.