Posts Tagged Active Directory
SecurityException was unhandled by user code – VSTA, C#, InfoPath 2007
Posted by Rajanihanth in .Net, Active Directory, DirectoryServices, InfoPath 2007, InfoPath 2010, VSTA on July 18, 2012
I got this error, while I was developing a InfoPath form in VSTA (Visual Studio Tools for Applications 2008) . This was my enviornment:
- Windows 7
- VSTA 2008
- InfoPath 2007
Scenario: I was trying to get the user information from Active Directory(AD) and then display in an InfoPath form. Here is the C# code I have written in the VSTA:
public void CTRL1_5_Clicked(object sender, ClickedEventArgs e) { string strUserName = System.Environment.UserName; string xpath2 = "/my:myFields/my:field1"; XPathNavigator field2 = MainDataSource.CreateNavigator().SelectSingleNode(xpath2, NamespaceManager); field2.SetValue(GetOU(strUserName)); //Get Organization Unit(OU) from Acitve Directory(AD) }
This is the error message I have got: 😦
Solution: I have fixed this issue in a different situation, just read in my previous post.
Thanks. R./
Happy InfoPathing. 🙂
How to get the current user’s OU (Organizational Unit) from AD (Active Directory) – C#
Posted by Rajanihanth in .Net, Active Directory on February 7, 2012
There are so many ways to get the user details form AD (Active directory), but I wanted to get the current user’s OU (Organizational Unit) from Active directory. There is no any direct method to get the OU (Actually I could not find anything on the web, if anyone get an easy way to find-out please let me know). I am currently developing a Custom Search for SharePoint 2007 and according to the OU, I want to display the search results.
Here is the method I have created and most of the comments I have put in the code itself. I used Asp.net and C#.
public string GetOU(string username) { string result = string.Empty; using (HostingEnvironment.Impersonate()) { //Getting the domain PrincipalContext yourDomain = new PrincipalContext(ContextType.Domain); //Finding the user UserPrincipal user = UserPrincipal.FindByIdentity(yourDomain, username); //If the user found if (user != null) { // Getting the DirectoryEntry DirectoryEntry directoryEntry = (user.GetUnderlyingObject() as DirectoryEntry); //if the directoryEntry is not null if (directoryEntry != null) { //Getting the directoryEntry's path and spliting with the "," character string[] directoryEntryPath = directoryEntry.Path.Split(','); //Getting the each items of the array and spliting again with the "=" character foreach (var splitedPath in directoryEntryPath) { string[] eleiments = splitedPath.Split('='); //If the 1st element of the array is "OU" string then get the 2dn element if (eleiments[0].Trim() == "OU") { result = username + "-" + eleiments[1].Trim(); break; } } } } } return result; }
Please don’t forget to add the following references:
using System.Collections; using System.DirectoryServices.AccountManagement; using System.DirectoryServices; using System.Security.Principal;
If you want to get the current user’s OU then you can call this method in the Page_Load event.
protected void Page_Load(object sender, EventArgs e) { string userName = Context.User.Identity.Name; LabelOU.Text = GetOU(userName); }
You will get the output like this:
OU-Name
References: