[英]How to programatically check if a user can access a SharePoint site?
I have a service that runs on the save server as SharePoint. 我有一个在保存服务器上作为SharePoint运行的服务。 I can use this service to upload/move files in document libraries on different SharePoint sites.
我可以使用此服务在不同SharePoint网站上的文档库中上载/移动文件。 I am now trying to check if a user (DOMAIN\\USER) has permissions to access a particular SharePoint site.
我现在正在尝试检查用户(DOMAIN \\ USER)是否具有访问特定SharePoint网站的权限。
I have tried using the following: 我尝试使用以下方法:
web.EnsureUser("DOMAIN\USER")
web.CheckPermissions(SPBasePermissions.Open)
The above should raise UnauthorizedAccessException if the user has no permissions. 如果用户没有权限,以上内容将引发UnauthorizedAccessException。 For me it never raises any exception although that user does not have permissions to access this particular site (verified by trying to access the site on the user's machine - Unauthorized 401)
对我来说,它永远不会引发任何异常,尽管该用户没有访问该特定站点的权限(通过尝试访问用户计算机上的站点进行验证-未经授权401)
web.DoesUserHavePermissions("DOMAIN\\USER", SPBasePermissions.Open)
The above should return True or False, but for me it always returns False, also when the user has permissions to access a site (verified by accessing the site on the user's machine - OK 200). 上面的应该返回True或False,但对我来说,它总是返回False,同样,当用户有权访问站点时(通过访问用户计算机上的站点进行验证-确定200)。
web.GetUserEffectivePermissions("DOMAIN\\USER")
The above should return the permissions mask, but it always returns an EmptyMask. 上面应该返回权限掩码,但始终返回EmptyMask。
I think I do not understand those methods, but they are not described well anywhere on the Internet. 我认为我不理解这些方法,但是在Internet上的任何地方都没有很好地描述它们。
Does anyone know how I can check if a user has permissions necessary to access a SharePoint site? 有谁知道我如何检查用户是否具有访问SharePoint网站所需的权限?
I had the same problem as yours. 我和你有同样的问题。 I was passing username in format "domain\\username" but it was returning all permissions as false.
我正在以“ domain \\ username”格式传递用户名,但它会将所有权限返回为false。 Finally I realized that I was using Claims based authentication for SharePoint.
最终,我意识到我正在对SharePoint使用基于声明的身份验证。 I changed username to claims format "i:0#.w|domain\\username" and it started working perfectly after that.
我将用户名更改为声明格式“ i:0#.w | domain \\ username”,此后它开始正常运行。 Hope this helps.
希望这可以帮助。
Here is a sample working code - 这是一个示例工作代码-
var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);
using (var clientContext = spContext.CreateUserClientContextForSPHost())
{
string userName = @"i:0#.w|domain\username";
var userPermissions = clientContext.Web.GetUserEffectivePermissions(userName);
clientContext.ExecuteQuery();
foreach (var permission in Enum.GetValues(typeof(PermissionKind)).Cast<PermissionKind>())
{
var permissionName = Enum.GetName(typeof(PermissionKind), permission);
var hasPermission = userPermissions.Value.Has(permission);
Response.Write(String.Format("<br>Permission: {0}, HasPermission: {1}", permissionName, hasPermission));
}
}
Source - http://tech-turf.blogspot.in/2015/12/sharepoint-2013-getusereffectivepermiss.html 来源-http://tech-turf.blogspot.in/2015/12/sharepoint-2013-getusereffectivepermiss.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.