Why doesn't this work if in a UserControl?

J

Jason .

This code comes directly from Microsoft's site:

Imports System.Security

Add the following code to the Page_Load event procedure:

Dim authUserName As String
Dim aspUserName As String
authUserName = User.Identity.Name
aspUserName = Principal.WindowsIdentity.GetCurrent.Name

If I put the code in a webform (.aspx file) it works. But, if I try to
put it in a user control, I get the little blue squigly line under the
word User. (right before .Identity.Name). Why is that?
 
K

Karl Seguin

Because user is a property of the Page class. so in the page, when you are
doing

User.Identity.Name

it's really like doing

me.User.Identity.Name

and since me is an instance of Page, it inherits the User property.

A user control inherits from UserControl, instead of Page, and doesn't have
the property. To access the page's user from the user control do:
Page.User.Identity.Name ...as you can see while User isn't a property of
UserCOntrol, Page is...

Karl
 
M

Matt Berther

Hello Karl Seguin,

The other way to go would be to reference:

HttpContext.Current.User.Identity.Name
 

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

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,199
Latest member
AnyaFlynn6

Latest Threads

Top