C# 用于查询个人所有组的自定义属性存储

C# 用于查询个人所有组的自定义属性存储,c#,active-directory,adfs,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.ClaimsPo

有人能帮我写代码吗?我需要一个自定义属性存储来查询一个人的所有组

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

我的代码是这样的:

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)
        {

        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.DirectoryServices;
使用System.Threading.Tasks;
使用Microsoft.IdentityServer.ClaimsPolicy.Engine.AttributeStore;
使用System.IdentityModel;
命名空间GroupClauditPropertyStore
{
公共类GroupClaimStore:IAttributeStore
{
公共IAsyncResult BeginExecuteQuery(字符串查询、字符串[]参数、异步回调、对象状态)
{
DirectorySearcher search=new DirectorySearcher(new DirectoryEntry(“uk.svc.com”){AuthenticationType=AuthenticationTypes.Secure,Path=“LDAP://OU=staffusers,DC=leeds art,DC=ac,DC=uk”});
//获取用户名
string username=string.Empty;
//设置过滤器
search.Filter=“(cn=“+username+””);
SearchResult=search.FindOne();
search.PropertiesToLoad.Add(“memberOf”);
List outputValue=新列表();
如果(结果!=null)
{
ResultPropertyCollection字段=result.Properties;
变量组=字段[“memberOf”];
foreach(组中的var组)
{
Add(group.ToString());
}
}
TypedAsyncResult asyncResult=新的TypedAsyncResult(回调,状态);
asyncResult.Complete(outputValue.ToArray(),false);
返回结果;
}
公共字符串[][]EndExecuteQuery(IAsyncResult结果)
{
返回类型dasyncresult.End(结果);
}
公共void初始化(字典配置)
{
}
}
}

非常感谢你

您可以通过LDAP规则将“令牌组-非限定名称”映射到“角色”来实现这一点

否则,没有名为“username”的广告属性。您可能需要“sAMAccountName”