Solved

User.Identity.Name not working in master page codebehind

Posted on 2007-11-29
12
2,498 Views
Last Modified: 2012-06-27
I have placed this in the codebehind of a regular page:

Label3.Text = User.Identity.Name

It works.

But if I try to use the User.Identify.Name in the codebehind of a master page (Page_Load), it says:

Compiler Error Message: BC30451: Name 'IsUserInRole' is not declared.
If IsUserInRole(User.Identity.Name, "Admin") Then

What must I do to get this to work within my master page?

thanks.
0
Comment
Question by:Starr Duskk
  • 7
  • 5
12 Comments
 
LVL 7

Expert Comment

by:Infinite_Recursion
ID: 20377523
is that page inheriting from System.Web.UI.Page or another class in the project? I am guessing this error has to do with code in that base page class.
0
 
LVL 2

Author Comment

by:Starr Duskk
ID: 20377553
It's a master page and:

Imports System.Web

Partial Class _MyMaster
    Inherits System.Web.UI.MasterPage

I tried to Inherits System.Web.UI.Page, but it still gives the same error line.

I did Imports System.Web.UI.Page and it still squiggly lines the
0
 
LVL 7

Expert Comment

by:Infinite_Recursion
ID: 20377585
I was talking about the page that uses the masterpage not the MasterPage itself, where is this code located:

If IsUserInRole(User.Identity.Name, "Admin") Then

???
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 2

Author Comment

by:Starr Duskk
ID: 20377794
The code that uses it and works, it's in the Page_Load.

But I am trying to put it in the Page_Load of the master page.

I want to call this on all pages, not paste it for every page on the site.
0
 
LVL 7

Expert Comment

by:Infinite_Recursion
ID: 20377818
try
If Page.IsUserInRole(User.Identity.Name, "Admin") Then
0
 
LVL 2

Author Comment

by:Starr Duskk
ID: 20378043
Sorry, no.

Compiler Error Message: BC30456: 'IsUserInRole' is not a member of 'System.Web.UI.Page'.
If Page.IsUserInRole(User.Identity.Name, "Admin") Then
0
 
LVL 7

Expert Comment

by:Infinite_Recursion
ID: 20378129
please post the working code for me
0
 
LVL 2

Author Comment

by:Starr Duskk
ID: 20378314

Imports System.Web
 
 
Partial Class _MasterPage
    Inherits System.Web.UI.MasterPage
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
 
        If Page.IsUserInRole(User.Identity.Name, "Admin") Then
        End If
 
    End Sub
End Class

Open in new window

0
 
LVL 2

Author Comment

by:Starr Duskk
ID: 20378328
Or with this, it gives: "Name 'User' is not declared"

        If Roles.IsUserInRole(User.Identity.Name, "Admin") Then

        End If
0
 
LVL 7

Accepted Solution

by:
Infinite_Recursion earned 500 total points
ID: 20378409
f Roles.IsUserInRole(Page.User.Identity.Name, "Admin") Then

try this
0
 
LVL 2

Author Comment

by:Starr Duskk
ID: 20380074
Sorry, no. same thing.
0
 
LVL 2

Author Comment

by:Starr Duskk
ID: 20380125
I took your last thing and put it in the Pre_Init:

    ' the Master Page PreInit Fires before the PageLoad each page
    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

        If Roles.IsUserInRole(Page.User.Identity.Name, "Admin") Then
            Label1.Text = "Label3"
        End If

    End Sub

It works now. So the "Page." and the Pre_Init are the combined solution.

thanks!
0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Run time Error 4 48
Delete HTML table rows 12 29
Moving code from one TFS branch to another 3 25
Example works but why do I need to click twice on button 4 42
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question