Solved

User.Identity.Name not working in master page codebehind

Posted on 2007-11-29
12
2,504 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

830 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