自定义属性存储以查询人员的所有组

时间:2017-05-08 01:15:21

标签: c# active-directory adfs

有人可以帮我处理我的代码吗?我需要一个自定义属性存储来查询一个人的所有组。

我不知道如何获取访问依赖方的人的用户名。

我的代码是这样的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
using System.Threading.Tasks;
using Microsoft.IdentityServer.ClaimsPolicy.Engine.AttributeStore;
using System.IdentityModel;


namespace GroupClaimAttributeStore
{
    public class GroupClaimStore : IAttributeStore
    {
        public IAsyncResult BeginExecuteQuery(string query, string[] parameters, AsyncCallback callback, object state)
        {
            DirectorySearcher search = new DirectorySearcher(new DirectoryEntry("uk.svc.com") { AuthenticationType = AuthenticationTypes.Secure, Path = "LDAP://OU=staffusers,DC=leeds-art,DC=ac,DC=uk" });
            // get username
            string username = string.Empty;
            // set filter
            search.Filter = "(cn=" + username + ")";

            SearchResult result = search.FindOne();
            search.PropertiesToLoad.Add("memberOf");

            List<string> outputValue = new List<string>();

            if (result != null)
            {
                ResultPropertyCollection fields = result.Properties;
                var groups = fields["memberOf"];
                foreach (var group in groups)
                {
                    outputValue.Add(group.ToString());
                }
            }

            TypedAsyncResult<string[]> asyncResult = new TypedAsyncResult<string[]>(callback, state);
            asyncResult.Complete(outputValue.ToArray(), false);
            return asyncResult;
        }

        public string[][] EndExecuteQuery(IAsyncResult result)
        {
            return TypedAsyncResult<string[][]>.End(result);
        }

        public void Initialize(Dictionary<string, string> config)
        {

        }
    }
}

非常感谢你!

0 个答案:

没有答案