Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

User.Identity.Name not working in master page codebehind

Posted on 2007-11-29
12
Medium Priority
?
2,595 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

604 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