简体   繁体   English

如何以编程方式检查用户是否可以访问SharePoint网站?

[英]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.

相关问题 如何检查用户是否可以在Sharepoint 2013中下载文件? - How to check if user can download file in Sharepoint 2013? 打开网站集功能以编程方式sharepoint csom - turn on site collection features programatically sharepoint csom 在SharePoint 2010中以编程方式创建网站工作流? - Programatically creating a Site Workflow in SharePoint 2010? 如何通过Windows应用程序访问具有基于声明的身份验证的SharePoint 2010网站页面? - How can I access SharePoint 2010 site page which has claim based authentication through windows application? 如何将当前特定于用户的主题应用于共享站点 - How to apply a current user specific theme to a sharepoint site 如果我以编程方式加载它,如何访问用户控件属性? - How to access user control properties if i load it programatically? 当用户拥有以前有效的cookie时,如何删除对网站的访问? - How to remove access to site when the user had a previously valid cookie? 如何以编程方式创建DataTable? - How can a create a DataTable programatically? 验证网络凭据以访问客户端对象模型上的SharePoint站点 - Authenticate network credential to access SharePoint site on client object model 如何将WebPart添加到SharePoint网站中的所有页面? - how to add a WebPart to all pages in a SharePoint site?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM