Solved

User.Identity.Name not working in master page codebehind

Posted on 2007-11-29
12
2,478 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:BobCSD
  • 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 1

Author Comment

by:BobCSD
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
 
LVL 1

Author Comment

by:BobCSD
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 1

Author Comment

by:BobCSD
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 7

Expert Comment

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

Author Comment

by:BobCSD
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 1

Author Comment

by:BobCSD
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 1

Author Comment

by:BobCSD
ID: 20380074
Sorry, no. same thing.
0
 
LVL 1

Author Comment

by:BobCSD
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

760 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now