One thing that I don’t understand about is your question about retrieving the domain name specified in the directory in the domain controller. I assume that you have a server that can see several trusted domains, and that the user can log into your application from any of them, so that you do not know which domain you need to check for membership in the role.
To control access to functions through membership in ADGroup, you can use
HttpContext.Current.User.IsInRole("appdomain\groupname")
where User.Identity.Name == "userdomain \ user". I am not familiar with domain trust issues, but this assumes that you can add users from a trusted domain to the domain group that you control, so you don't have to worry about the location of the domain group.
If you can’t or you have the same group name in every other domain, can you do something like this?
HttpContext.Current.User.IsInRole(userDomainname + "\groupname")
Some moments:
- if you don’t already have a large AD database, I would recommend using objects from the System.DirectoryServices.AccountManagement namespace.
- I highly recommend the ADExplorer utility from Sysinternals to get a more LDAP representation of your domain (s), which helps with the chains and directory of the LDAP programming connection in general.
- If you are comfortable working with interop and need to do any parsing of the LDAP string, check out this site .
- Properties of System.DirectoryServices.AccountManagement. PrincipalContext.Container and System.DirectoryServices. DirectoryEntry.Path returns the LDAP connection string with the domain at the end of the string (i.e. DC = mycompany, DC = com)
- Don't forget about the robust old Environment.UserDomainName and Environment.UserName (which grabs the WindowsPrincipal from the current executable thread, see Table 1: Object implemented in the CurrentPrincipal Object @ http://msdn.microsoft.com/en-us/library thread /Aa480475.aspx for an excellent table stating that the current user is in the asp.net runtime.)
** UPDATE 6/8/2011 2:15 PM **
If I understand AD correctly, the user domain is an integral part of the user object returned by AD. Extension on your example "Bob Newaccountant" ...
So, given the following 2 Domains with trust between them:
1. "abcdc.com" CN=Users CN="Bob NewAccountant" 2. "abc.com" CN=Users CN="Local User1" OU=Applications OU=MyApplication CN=ReportReaders (Members: abcdc\BNewAccountant, abc\luser1)
You should get user information requesting the following query:
//name parameter = domain //container parameter = distinguished name using(var ctx = new PrincipalContext( ContextType.Domain, name: "abc.com", container: "OU=MyApplication,OU=Applications,DC=abc,DC=com", "abc\serviceaccountname", "Password1")) { var officeGroup = GroupPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, "ReportReaders"); foreach(Principal prin in officeGroup.GetMembers(recursive: true)) { Console.WriteLine("DistinguishedName: " + prin.DistinguishedName + " UPN: " + prin.UserPrincipalName); } //Should result in // DistinguishedName: CN=luser1,CN=Users,DC=abc,DC=com UPN: luser1@abc.com // DistinguishedName: CN=BNewAccountant,CN=Users,DC=abcdc,DC=com UPN: BNewAccountant@abcdc.com }
This way you can get the user domain through the distinguished name or userPrincipalName of the active directory property. (Note. I don’t have the settings for the two domains, so I can’t check the above code at this time.) Is this closer?