Starr Duskk
asked on
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.
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
What must I do to get this to work within my master page?
thanks.
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.
ASKER
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
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
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
???
If IsUserInRole(User.Identity
???
ASKER
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.
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.
try
If Page.IsUserInRole(User.Ide ntity.Name , "Admin") Then
If Page.IsUserInRole(User.Ide
ASKER
Sorry, no.
Compiler Error Message: BC30456: 'IsUserInRole' is not a member of 'System.Web.UI.Page'.
If Page.IsUserInRole(User.Ide ntity.Name , "Admin") Then
Compiler Error Message: BC30456: 'IsUserInRole' is not a member of 'System.Web.UI.Page'.
If Page.IsUserInRole(User.Ide
please post the working code for me
ASKER
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
ASKER
Or with this, it gives: "Name 'User' is not declared"
If Roles.IsUserInRole(User.Id entity.Nam e, "Admin") Then
End If
If Roles.IsUserInRole(User.Id
End If
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sorry, no. same thing.
ASKER
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.Us er.Identit y.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!
' 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.Us
Label1.Text = "Label3"
End If
End Sub
It works now. So the "Page." and the Pre_Init are the combined solution.
thanks!