質問

入れは行っていませんで繰り返し処理グループが与えられたユーザのグループ?

私が使用できますIsInRoleにWindowsPrincipalオブジェクトが何らかの理由でないも働かされているとは思いませんが、エラーやexceptionがスローされるものでfalseを返します。

について以下のコードをwebからでも助けてくれ改善し信頼性の面でいたので間違った結果を3週間の試験をします。

側の注意1:思っていたアクセスコンピューター-プログラムがユーザー名とパスワードを使用します。2:グループを作成でき作成されたドメインが同じです。3:グループのユーザーからの様々な領域としています。

感謝

KA

    [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
    static extern int CheckTokenMembership(int TokenHandle, byte[] PSID, out bool IsMember);

   [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
    static extern bool IsValidSid(byte[] PSID);


private bool Authenticate(XmlNodeList XmlNodeGroups)
    {
        bool result = false;
        try
        {
            Dictionary<string, List<string>> Groups = GetGroups(XmlNodeGroups);
            //search global catalog and get SID of the group
            Byte[] sid = null;
            foreach (string groupName in Groups.Keys)
            {
                using (DirectoryEntry entry = new DirectoryEntry("GC:"))
                {
                    IEnumerator ie = entry.Children.GetEnumerator();
                    ie.MoveNext();
                    using (DirectorySearcher ds = new DirectorySearcher((DirectoryEntry)ie.Current))
                    {
                        ds.Filter = string.Format("(&(|(sAMAccountName={0}))(objectClass=group))", groupName);  
                        using (SearchResultCollection resColl = ds.FindAll())
                        {
                            if (resColl.Count > 0)
                            {
                                ResultPropertyCollection resultPropColl = resColl[0].Properties;
                                sid = (byte[])resultPropColl["objectsid"][0];
                                if (sid == null || !IsValidSid(sid))
                                {
                                    // log message and continue to next group                                        continue;
                                }
                            }
                            else
                            {
                                  // log message and continue to next group                                    continue;
                            }
                        }

                        bool bIsMember = false;
                        if (CheckTokenMembership(0, sid, out bIsMember) == 0)
                        {
                               // log message and initiate fall back....... use Legacy
                            result = CheckMemberOf(XmlNodeGroups, _CurrentIdentity);
                            break;
                        }
                        else
                        {
                            result = bIsMember ? true : false;
                            if (result)
                            {
                                // debug message                                    break;
                            }
                            else
                            {
                               // debug message
                            }
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            // log exception message and initiate fall back....... use Legacy
            result = CheckMemberOf(XmlNodeGroups, _CurrentIdentity);
        }
        return result;
    }</code>
役に立ちましたか?

解決

ています。純3.5?その場合、チェックアウトは、MSDNの雑誌記事 管理ディレクトリの安全を養います。NET Framework3.5.でかくないユーザーグループの広告です。

など充実した設備でお客様の旅行条件-き

  • のグループか
  • 列挙すべての会員
  • されている場合は、見つけされたユーザーは、会員がグループ

とを行うことができ、非常に簡単には System.DirectoryServices.AccountManagement 名前:

// establish a context - define a domain (NetBIOS style name),  
// or use the current one, when not specifying a specific domain
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

// find the group in question
GroupPrincipal theGroup = GroupPrincipal.FindByIdentity(ctx, "nameOfGroup");

// recursively enumerate the members of the group; making the search
// recursive will also enumerate the members of any nested groups
PrincipalSearchResult<Principal> result = theGroup.GetMembers(true);

// find the user in the list of group members
UserPrincipal user = (result.FirstOrDefault(p => p.DisplayName == "Some Name") as UserPrincipal);

// if found --> user is member of this group, either directly or recursively
if(user != null)
{
     // do something with the user
}

他のヒント

私は3.5フレームワークの上に自分のコードスニペットを使用しようと、この行は私のコンパイラが、それは正しくないと言うます:

    // find the user in the list of group members
    UserPrincipal user = (result.FirstOrDefault(p => p.DisplayName == adUser) as UserPrincipal);

具体的にresult.FirstOfDefault、それはそれは有効なオプションではありませんと言います。

ありがとうございます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top