Why doesn't this work if in a UserControl?

  • Thread starter Thread starter Jason .
  • Start date Start date
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?
 
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
 
Hello Karl Seguin,

The other way to go would be to reference:

HttpContext.Current.User.Identity.Name
 
Back
Top