Restrict access to a WPF view based on AD group membership

笑着哭i 提交于 2019-12-10 14:19:31

问题


We have a WPF application. We would like to resrict access to the application based on the users AD group membership.

Could we do this as an attribute on each view, or as a check when the user starts the application?

Any code example would be appreciated.


回答1:


The easiest way to do this on .NET 3.5 and up would be to use the System.DirectoryServices.AccountManagement (S.DS.AM) namespace. Read all about it here:

  • Managing Directory Security Principals in the .NET Framework 3.5
  • MSDN docs on System.DirectoryServices.AccountManagement

Basically, you can define a domain context and easily find users and/or groups in AD:

// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

// get your group in question
GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "YourGroupNameHere");

// check if current user is member of that group
UserPrincipal user = UserPrincipal.Current;

if(user.IsMemberOf(group))
{
   // do something here....     
}

The new S.DS.AM makes it really easy to play around with users and groups in AD!



来源:https://stackoverflow.com/questions/8971128/restrict-access-to-a-wpf-view-based-on-ad-group-membership

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!