• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2607
  • Last Modified:

User.Identity.Name not working in master page codebehind

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
Starr Duskk
Asked:
Starr Duskk
  • 7
  • 5
1 Solution
 
Infinite_RecursionCommented:
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
 
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
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
 
Infinite_RecursionCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
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
 
Infinite_RecursionCommented:
try
If Page.IsUserInRole(User.Identity.Name, "Admin") Then
0
 
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
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
 
Infinite_RecursionCommented:
please post the working code for me
0
 
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:

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
 
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
Or with this, it gives: "Name 'User' is not declared"

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

        End If
0
 
Infinite_RecursionCommented:
f Roles.IsUserInRole(Page.User.Identity.Name, "Admin") Then

try this
0
 
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
Sorry, no. same thing.
0
 
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
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

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.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now