0

我正在嘗試讀取group groupType屬性的SECURITY_ENABLED標誌。問題是,值我用讀取Active Directory中Group的SECURITY_ENABLED標誌

DirectoryEntry entry... 
entry.Properties["groupType"].Value; 

int32 retreive,其範圍是-2,147,483,647 to 2,147,483,648 (or -0x7FFFFFFF to 0x80000000)

quickWatch

所以隨時存在GROUP_TYPE_SECURITY_ENABLED和任何其他arbitraty標誌設置,數值超出了int32的範圍併發生溢出。

groupTypeScreen

有誰知道如何避免爲了讀取正確的值這個溢出?

+0

國旗上有是一個int 0x80000000的適合的單位很好。 – urbanlemur

+1

默認情況下,Int32已簽名。你可以使用UInt32? – fourpastmidnight

+0

Entry.Properties [「groupType」]。Value返回-2147483646。當我嘗試將其轉換爲UINT我得到一個InvalidCastException – wodzu

回答

1

我認爲你需要使用UInt32爲您列舉GroupType的類型如下:

[Flags] 
public enum GroupType : uint 
{ 
    BUILTIN_LOCAL_GROUP = 0x00000001, 
    ACCOUNT_GROUP  = 0x00000002, 
    RESOURCE_GROUP  = 0x00000004, 
    UNIVERSAL_GROUP  = 0x00000008, 
    APP_BASIC_GROUP  = 0x00000010, 
    APP_QUERY_GROUP  = 0x00000020, 
    SECURITY_ENABLED = 0x80000000 
} 

讓我知道是否能解決你的問題。

編輯:好的,我不確定Entry是您創建的對象還是Active Directory API的一部分。話雖如此,我快創建以下變量中的一個項目,我目前正在和編譯如下:

// I only made it static so I could get my compiler to compile this in something I'm currently 
// working on. It's not necessary for it to be static. 
static int SECURITY_ENABLED = 0x80000000; 

int newValue = SECURITY_ENABLED | 1; 

我沒有得到任何編譯時錯誤。實際上,重新查看價值0x80000000,它的範圍是Int32

重新審視上面的代碼,確切地說,你會得到錯誤嗎?我看到這個suspicios代碼:

if (groupTypes.Count == 1) 
{ 
    var firstFlag = (int) groupTypes[0]; 

    // What is this checking, exactly? 
    // Is this where the error is occurring? 
    var longFlag = -(((long) flag) - firstFlag); 

    if ((longFlag == 0x80000000)) // Extra parentheses here...just a formatting thing 
     groupTypes.Add(GroupType.SECURITY_ENABLED); 
} 

也許這個代碼可以簡化?

public List<GroupType> GetGroupType() 
{ 
    var groupTypes = new List<GroupType>(); 
    var flag = (GroupType) this.Entry.Properties["groupType"].Value; 

    if (flag & GroupType.ACCOUNT_GROUP > 0) 
     groupTypes.Add(GroupType.ACCOUNT_GROUP; 
    else if (flag & GroupType.APP_BASIC_GROUP > 0) 
     groupTypes.Add(GroupType.APP_BASIC_GROUP); 

    // ... Other else if ad nauseum ... 

    else if (flag & GroupType.SERUCITY_ENABLED > 0) 
     groupTypes.Add(GroupType.SECURITY_ENABLED); 

    return groupTypes; 
} 

如果你真的需要無論出於何種原因的ArrayList(),然後你可以只是做return groupTypes.ToArray<int>();

HTH。

+0

前面已經說了:Entry.Properties [「groupType」]值返回-2147483646。當我嘗試將它轉換爲uint時,我得到一個InvalidCastException。所以我不能,即使在調試模式下枚舉 – wodzu

+0

你可以看到結合返回值,它是一個符號整數 http://img845.imageshack.us/img845/2369/grouptype.png – wodzu

+0

的「可疑」的代碼是一種解決方法,即使有緩衝區溢出,也可以讀取標誌。現在它可以工作,如果有兩個標誌設置。首先我得到這個標誌,那不是SERUCITY_ENABLED標誌。然後我轉換給定的flagvalue並將其轉換爲long以便通過減去第一個標誌的值來恢復初始值,以便我可以檢查SERUCITY_ENABLED標誌是否已設置。具體來說,我得到一個收到COMException,當我嘗試,以設定標記是這樣的: VAR VAL =(長)((int)的this.Entry.Properties [ 「groupType」]值。); \t \t \t \t \t this.Entry.Properties [ 「groupType」]值= VAL&〜0x80000000的。 – wodzu

1

引用@ fourpastmidnight的答案,這些文章object-group-attribute-grouptypemsdn Group-Type attribute,我能夠找到不需要鑄造uint或通過if ... else if語句解析解。

看到第一個鏈接的負值和@ wodzu對-2147483646返回值的評論,我嘗試將SECURITY_ENALBED值反轉爲-0x80000000。

[System.Flags] 
public enum GroupType 
{ 
    BUILTIN_LOCAL_GROUP = 0x00000001, 
    ACCOUNT_GROUP  = 0x00000002, 
    RESOURCE_GROUP  = 0x00000004, 
    UNIVERSAL_GROUP  = 0x00000008, 
    APP_BASIC_GROUP  = 0x00000010, 
    APP_QUERY_GROUP  = 0x00000020, 
    SECURITY_ENABLED = -0x80000000 
} 

現在所獲得的價值時,和鑄件,GroupType

var groupType = (GroupType)this.Entry.Properties["groupType"].Value 

然後,您可以在GroupType值將返回每個標誌的逗號分隔字符串中使用的ToString()。
或者你可以使用。檢查HasFlag方法,如果它是一個安全組

bool IsSecurityGroup = groupType.HasFlag(GroupType.SECURITY_ENABLED);