I’m looking for a way to easily wrap security around a location, while at the same time using that to show/hide links on my menu.
I know how to block the location for unauthorized users, like:
<location path="AdminDirectory">
<system.web>
<authorization>
<allow roles="SomeRole"/>
<deny users="*"/>
</authorization>
</system.web>
</location>
And I can show/hide links with code, like:
myLink.Visible = User.IsInRole("SomeRole");
But what I’m looking for is a way to do this so that the “SomeRole” role name only has to be configured once. If I could read from the location element and test it in code, that would be ideal, but I’m not seeing a way to do that.
// Something like:
myLink.Visible = TestLocationAccess("AdminDirectory");
I can think of a really crappy way of doing this with a javascript/AJAX call to “test” the directory from the client and show/hide the link with javascript, but obviously that’s not ideal.
Thanks
You could use the CheckUrlAccessForPrincipal method:
Since the method is static you obviously need to pass as second argument an instance of the current
IPrincipal.