title: How to check if a user has permission to a SharePoint site collection
body: <p>What is the best way to check if a user has permission to a site collection/site? I'm currently using the following</p>
  
  <pre><code>   SPSecurity.RunWithElevatedPrivileges(
         () =&gt; {using (var site = new SPSite(nodeUrl))
                       {
                           using (var web = site.OpenWeb())
                           {
                               retValue=
                                   web.DoesUserHavePermissions(
                                       context.User.Identity.Name,
                                       SPBasePermissions.Open);
                           }
                       }
               });
  </code></pre>
  
  <p>This doesn't seem to be working properly. If the user was never added to the site this works. But if the user was added and then removed <code>DoesUserHavePermission(.. SPBasePermission.Open)</code> still returns true, but when the user tries to access the site SharePoint throws the access denied page.</p>
  
  <p>After a little more digging I found that the user account is still in the <code>web.AllUsers</code> list, but it has no Roles assigned.</p>