如何确定任意 DOMAIN\用户名是否属于 C# 的特定角色?
我对 C# 真的很陌生,所以请原谅我的无知。我需要测试用户(域\用户名)是否位于特定组中,是的,这包括嵌套组。
我发现在处理当前登录用户时,WindowsPrincipal.IsInRole()
效果非常好。但我的情况并非如此。我需要能够传入任意 DOMAIN\username 或 UPN(我将采用最容易实现的方式),并且如果它们是 X 组的成员,则返回 true/false,即使它们只是 X 组的间接成员组 X(例如:用户是组 Y 的成员,组 Y 是组 X 的成员)。
我研究过 WindowsIdentity
,也许它对 C# 来说是新的,但我只是没有找到一种方法来执行 WindowsIdentity("MYDOMAIN\User1")
之类的操作。嗯,我做到了,但从未让它发挥作用。
使用 C#,给定 DOMAIN\username,这不是当前登录的用户,我如何确定他们是否是 DOMAIN\group 的成员?
I'm really new to C# so forgive my ignorance. I need to test if a user (DOMAIN\username) is in a particular group, and yes, this includes nested groups.
I have found that WindowsPrincipal.IsInRole()
works fantastic, when dealing with the current logged-in user. That isn't the case for me though. I need to be able to pass in an arbitrary DOMAIN\username or UPN (I'll do whichever is easiest to implement), and get back true/false if they are a member of group X, even if they are only indirect members of group X (e.g: user is member of group Y, and group Y is member of group X).
I've looked at WindowsIdentity
, and maybe it's being new to C#, but I just didn't see a way to do something like WindowsIdentity("MYDOMAIN\User1")
. Well, I did, but never got anywhere close to getting it to work.
Using C#, given a DOMAIN\username, which will not be the current logged-in user, how can I determine if they are a member of DOMAIN\group ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以为此使用 LDAP 查询。这是一篇好文章
Howto:(几乎)通过 C# 实现 Active Directory 中的所有内容
You can use LDAP query for that. Here is a good article
Howto: (Almost) Everything In Active Directory via C#
这是我使用过的一个有效的函数,您应该几乎可以按原样使用它。您可能必须创建
ParseUserDomain
但这非常简单:Here is an function I have use that works and you should be able to use it as is almost. You will probably have to create
ParseUserDomain
but that is pretty straight forward:我在一个递归查询中回答了Stack Overflow 中名为 Find Recursive 的类似条目使用 C# 的组成员身份 (Active Directory)。更改我在那里提供的代码可以让您做您想做的事情。
I answered with a recursive query in a similary entry in Stack Overflow called Find Recursive Group Membership (Active Directory) using C#. Changing the code I gave there can allow you to do what you want.
回答自己的问题:我尝试了所提供的解决方案,但并不是为了让它们发挥作用。请注意,我 100% 确定这是由于我对 C# 缺乏经验,与评论者发布的内容无关。爱并感谢所有提供帮助的评论者。
对我有用的是: http:// /ddkonline.blogspot.com/2010/05/how-to-recursively-get-group-membership.html
我确实必须做一些基本的调整才能使上述解决方案适合我的情况(更改例如 LDAP 参数),但它基本上有效。如果是组成员则返回 true,否则返回 false。我希望这能为未来的搜索者节省一些头发,因为我已经失去了一把。再次感谢所有发布帮助的人。
Answer to own question: I tried the solutions presented, and wasn't to get them to work. Note, I'm 100% sure this is due to my inexperience with C#, and not anything to do with what the commenters posted. Love and thanks to all the commenters who helped out.
What did work for me is this: http://ddkonline.blogspot.com/2010/05/how-to-recursively-get-group-membership.html
I did have to do some basic tweaks to make the above solution fit my situation (change the LDAP params, for example), but it basically worked. Returns true if member-of-group, false otherwise. I hope this saves future searchers some hair, as I've already lost a handfull. Thanks again to all who posted help.