C# method to get Active Directory properties -
C# method to get Active Directory properties -
i want create method accepts 2 arguments; first beingness username, , sec beingness name of active directory property return... method exists in separate class (sharedmethods.cs
) , works fine if define property name locally in method, can't workout how pass in sec argument.
here method:
public static string getaduserproperty(string suser, string sproperty) { principalcontext domain = new principalcontext(contexttype.domain); userprincipal user = userprincipal.findbyidentity(domain, suser); var property = enum.parse<userprincipal>(sproperty, true); homecoming user != null ? property : null; }
and code calling follows;
sdisplayname = sharedmethods.getaduserproperty(susername, "displayname");
currently enum.parse
throwing next error:
the non-generic method 'system.enum.parse(system.type, string, bool)' cannot used type arguments
i can work removing enum.parse
, , manually specifying property retrieve such:
public static string getaduserproperty(string suser, string sproperty) { principalcontext domain = new principalcontext(contexttype.domain); userprincipal user = userprincipal.findbyidentity(domain, suser); homecoming user != null ? user.displayname : null; }
pretty sure i'm missing obvious, in advance everyone's time.
because userprincipal not enum hard svein suggest possible reflections.
public static string getaduserproperty(string suser, string sproperty) { var domain = new principalcontext(contexttype.domain); var user = userprincipal.findbyidentity(domain, suser); var property = getpropvalue(user, sproperty); homecoming (string) (user != null ? property : null); } public static object getpropvalue(object src, string propname) { homecoming src.gettype().getproperty(propname).getvalue(src, null); }
c# active-directory
Comments
Post a Comment