zoukankan      html  css  js  c++  java
  • [sharepoint]根据用户名获取该用户的权限

    写在前面

    这样的一个场景,客户端请求sharepoint的rest api,但不允许传输用户的密码,使用的是证书认证的方式,但这样所有的用户用的是同一个证书,这样造成的结果就是无法识别该用户是否有操作,及查询的权限。这里是实际项目中遇到的一个问题。将解决方案,记录一下。

    解决方案

     try
                {
                    ClientContext spContext = new ClientContext("http://xxxx/xxx/xxx");
                    spContext.ExecutingWebRequest += spContext_ExecutingWebRequest;
                    var list = spContext.Web.Lists.GetByTitle("test");
                    spContext.Load(list);
                    spContext.ExecuteQuery();
                    var permissions = list.GetUserEffectivePermissions(@"i:0#.w|domain	est15");
                    spContext.ExecuteQuery();
                    foreach (var permission in Enum.GetValues(typeof(PermissionKind)).Cast<PermissionKind>())
                    {
                        var permissionName = Enum.GetName(typeof(PermissionKind), permission);
                        var hasPermission = permissions.Value.Has(permission);
                        Debug.WriteLine("Permission: {0}, HasPermission: {1}", permissionName, hasPermission);
                    }
                }
                catch (Exception)
                {
                    
                    throw;
                }

    在回调方法中,带上证书认证

     void spContext_ExecutingWebRequest(object sender, WebRequestEventArgs e)
            {
                HttpWebRequest webReq = e.WebRequestExecutor.WebRequest;
                var accessToken = TokenHelper.GetS2SAccessTokenWithWindowsIdentity(new Uri("http://xxx/xxx/xxxxx"), null);
                webReq.Method = "Post";
                webReq.Accept = "application/json;odata=verbose";
                webReq.Headers.Add("Authorization", "Bearer " + accessToken);
            }

    然后根据返回的权限,参考
    SPBasePermissions 枚举

    根据该枚举提供的值,判断该用户是否拥有权限。

  • 相关阅读:
    字符串匹配的KMP算法(转)
    二分查找谜题
    快排的优化--说说尾递归
    ZR提高失恋测4
    CF1209
    ZR普转提2
    ZR提高失恋测3
    ZR提高失恋测2(9.7)
    ZR9.8普转提
    CF1214
  • 原文地址:https://www.cnblogs.com/wolf-sun/p/4637126.html
Copyright © 2011-2022 走看看