How to check if a user has permission to a SharePoint site collection
Solution 1
Use CheckPermissions
instead of DoesUserHavePermissions
. See the SPWeb.CheckPermissions Method
.
Solution 2
I perform a similar check (looping over a list of workspaces checking for permissions), here is the relevant bit of code I use:
string LoginName = SPContext.Current.Web.CurrentUser.LoginName
bool permission = web.DoesUserHavePermissions(LoginName, SPBasePermissions.Open)
Satish
Updated on June 04, 2022Comments
-
Satish almost 2 years
What is the best way to check if a user has permission to a site collection/site? I'm currently using the following
SPSecurity.RunWithElevatedPrivileges( () => {using (var site = new SPSite(nodeUrl)) { using (var web = site.OpenWeb()) { retValue= web.DoesUserHavePermissions( context.User.Identity.Name, SPBasePermissions.Open); } } });
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
DoesUserHavePermission(.. SPBasePermission.Open)
still returns true, but when the user tries to access the site SharePoint throws the access denied page.After a little more digging I found that the user account is still in the
web.AllUsers
list, but it has no Roles assigned.