I wrote some web-based SharePoint code and put it under a new website in IIS (port 8080). It ran fine in the debugger, but under IIS I got this:
I tried
changing permissions, "SPSecurity.RunWithElevatedPrivileges", and a bunch of other things. Nothing.
I finally found
this post from Paul Galvin who suggested it was a security issue. His discovery that it had to do with the app pool gave me the idea to simply change my site's app pool to use the
same app pool as my SharePoint site. While it doesn't really
solve the underlying problem, it does work.