Tuesday 15 January 2013

asp.net - Active Directory group lookup function failing -


Help! I am trying to write a work that will confirm a user's membership in an Active Directory group, and it works when the user is in the group, the user does not give an exception.

Here's the job:

  using private bool ISUserMemberOfGroup (string user, string group) {(var ctx = new PrincipalContext (ContextType.Domain)) Group Principal = GroupPrincipal.FindByIdentity CTX, Group)) (var user principal = userpinnel.fisbiiindtte (ctx, user)) {if (group pinstrist == empty) {return false; } Other {Return User Principal ISMBER OFF (Group Principal); }}}  

And here is YSOD:

Server error in the '/' application.

Unknown error (0x80005000)

Description: for existing web request An unrestricted exception occurred during execution. For more information about the error, review the stack trace and where it started in the code.

Exception Details:

System. InteropServices.COMException: unknown error (0x80005000)

source error:

 line 34: other line 35: { Line 36: Return User Principal ISMBER OFF (GROUP PRINCIPAL); Line 37:} Line 38:}   

I do not know that it is related, but when I move through the function, GroupPrimary. Member. "Excludes the exception" System.NullReferenceException ", with the calculation.Bass shows an exception with the message" Object reference is not set as an example of an object. "

What is happening Is there a bool called IsMember just a false back when there is no member?

Thanks,

I think you can simplify things a bit:

  Private Bull ISUsmailber Group (String) C User, String Group) using {(var ctx = new PrincipalContext) (var user principal = userprintal.fundbiimenttte (ctx, user)) {principal search resultt & lt; principal> results = UserPrinter Group (); GroupPrinter GroupPinnal = Falling where (G = & gt; G.SackAccountName == GroupName). FirstOver Default (); Return (Group Principal! = Null);}}  

userPrincipal.GetGr Oups () will give you a definitive list of all group memberships (including primary group and nested group membership) for that user; Then search for the list of that group you are interested in, e.g.

If you search for the group you PrincipalSearchResult & lt; Principal & gt; by GetGroups () , your user is a member of that group.

You can save at least one "FindByIdentity" call with you.


No comments:

Post a Comment