Checking if member has accesss on content to create menu
I am trying to render the menu based on permission from public access..
@foreach (var primaryNavItem in primaryNavItems)
{
var isProtected = umbraco.library.IsProtected(primaryNavItem.Id, primaryNavItem.Path.ToString());
var loginAcces = isProtected && umbraco.library.HasAccess(primaryNavItem.Id, primaryNavItem.Path.ToString());
if (!isProtected || loginAcces)
{
//menu item here
}
}
But this throws error as below from log
System.NullReferenceException: Object reference not set to an instance of an object.
at Umbraco.Core.Services.PublicAccessServiceExtensions.HasAccess(IPublicAccessService publicAccessService, String path, MembershipUser member, RoleProvider roleProvider)
at Umbraco.Web.UmbracoHelper.MemberHasAccess(String path)
at Umbraco.Web.UmbracoHelper.MemberHasAccess(Int32 nodeId, String path)
at CallSite.Target(Closure , CallSite , Type , Object , Object )
at ASP._Page_Views_MasterLayout_cshtml.Execute() in e:\Demo\Src\MyApp\Views\MasterLayout.cshtml:line 80
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
at System.Web.WebPages.WebPageBase.<>c__DisplayClass3.<RenderPageCore>b__2(TextWriter writer)
at System.Web.WebPages.HelperResult.WriteTo(TextWriter writer)
at System.Web.WebPages.WebPageBase.Write(HelperResult result)
at System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action`1 body)
at System.Web.WebPages.WebPageBase.PopContext()
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
at Umbraco.Core.Profiling.ProfilingView.Render(ViewContext viewContext, TextWriter writer)
at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<>c__DisplayClass2b.<BeginInvokeAction>b__1c()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult)
at System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)
at System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult)
at System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult)
at System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
Can you please guide me right direction.
Umbraco is v7.3.1
Few more detail:
1. Members are Identity 2 based handled customly
2. Role provider is also custom and replaced umbraco default provider using web.config
One more thing, is there a way to get the roles name that is associated with a content, SO that I could check wiht current login user as I have role in session like
@foreach (var primaryNavItem in primaryNavItems)
{
var isProtected = umbraco.library.IsProtected(primaryNavItem.Id, primaryNavItem.Path.ToString());
var rolesAllowed=primaryNavItem.SomeMethod();
if (!isProtected || rolesAllowed.Contians(Session["myRole"])
{
//menu item here
}
}
Is there any way to accomplish, I really don't want to manipulate the umbraco table to check the permission for performance issue.
It looks like the problem with my custom membership. So I am thinking of this approach.
Checking if member has accesss on content to create menu
I am trying to render the menu based on permission from public access..
But this throws error as below from log
Can you please guide me right direction. Umbraco is v7.3.1
Few more detail: 1. Members are Identity 2 based handled customly 2. Role provider is also custom and replaced umbraco default provider using web.config
HI aits,
Since your are using Umbraco 7, then try have a look at the UmbracoHelper in Razor https://our.umbraco.org/documentation/Reference/Querying/UmbracoHelper/
As you can see you have Razor code examples
.MemberHasAccess(int nodeId, string path)
Returns a Boolean on whether the currently logged in member has access to the page given its ID and path.
.IsProtected(int pageId, string path)
Returns a Boolean on whether a page with a given pageId and path has public access restrictions set.
Hope this helps,
/Dennis
Hi Dennis,
Thanks, for your direction. But I have already tried this too. Even I got the same exception, so I tried with lower level methods but still no luck.
Hi Dennis,
Quick question, would this be the preferred method instead?
If I try to specify
page.Id
then Visual Studio complains that this is obsolete:Hi Bijesh,
You need to do something like this.
Hope this helps,
/Dennis
That's what I mean, when i use
@Umbraco.MemberHasAccess(page.Id, page.Path)
I get a message that it's obsolete.If I use just
Umbraco.MemberHasAccess(page.Path)
it seems fine and works ok.I'm wondering if the documentation needs updating?
I'm using 7.3.3.
Okay that for this information.
/Dennis
Hi Dennis,
One more thing, is there a way to get the roles name that is associated with a content, SO that I could check wiht current login user as I have role in session like
Is there any way to accomplish, I really don't want to manipulate the umbraco table to check the permission for performance issue. It looks like the problem with my custom membership. So I am thinking of this approach.
Any help could be great.
Thanks
is working on a reply...