R – Site members/visitors get access denied error page because of webpart

access-deniedpermissionssharepointweb-parts

I`v developed custom webpart. I can add it to page and site owners can view page with no problems.

However Site visitors/users get redirected to access denied error page. Sure, I must be doing some stuff normal users are not allowed to.

But the thing that bugs me is that when I place breakpoint in webpart constructor or even variable declaration it won't get hit! (If i try to view page with limited privileges). But in my browser, I get the access denied page. However, if I close that webpart, I instantly have access to that page and everything is OK.

What's the catch here? I was hoping I could catch exception with debugger to see which statement is guilty one, but it seems webpart doesn't execute any code and I get that exception.

Edit: Sorry, turns out debugger now hits those breakpoints. Don't know what changed, because symbols were loaded.

Best Answer

What is the trust level at your Sharepoint site ? You might want to give Fulltrust temporarily to see if you have any access problems.

If yes, then you might have a Code Access Security (CAS) problem on your hands. My first instinct is that your assembly does not have neccessary privilages to run correctly for normal users. You can configure it using custom CAS entry in the manifest.xml for the webpart, e.g. if you are calling external resources then you might have to configure System.New.WebPermission and define which url you are allowed to access. You can find more info on this at the U2U site.

Also what does your webpart do ? If you are trying to access resources which normal users don't have access to (e.g. a list in some site) then you have to use the SPSecurity.RunWithElevatedPrivilages method, so that you have the neccessary privilages (be careful to create new Site instance within that method so that the Context populates correctly).

Related Topic