hope this is the correct section - Forms authentication in ASP.NET

L

Leo Selmani

Hello. I have a bit of a dilemma. I have implemented a forms authentication
for my application built in asp.net and it's working fine so far. I was
trying to create a menu based on roles. I put th emenu in a .master page so
it is available to all pages. I was planning to do the permissions check in
the master page and allow users to view menu items accordingly. However I am
running into an issue, that certain members of the security context are not
available: The most important is 'User'. It is available to all other pages,
but not .master page. For example:

if (User.IsInRole("Administrator"))
hlinkAdmin.Visible = true;

The above will work on Default.aspx page
It will not work in main.master page, because it says that 'User' does not
exist in current context. I am using all the necessary namespaces

Any ideas?

Thanks,
Leo
 
S

Shaun C McDonnell

Leo,

The reason that the User is not available in the current context is most
likely because that User has not yet authenticated. You need to perform an
authentication check first, like this:

if(HttpContext.Current.IsAuthenticated)
{
hlinkAdmin.Visible = User.IsInRole("Administrator");
}

That should do it.

Shaun McDonnell
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top