Hi there,
I am trying to find/write a script in C#/ASPX that displays a users AD informaton on web page.
I am only after info such as username, name and email for example.
All the code snippets I have found so far are command line driven.
Thanks
M
Loading

VulpesRank #55 · 29.2K reputationPosted Aug 15, 2011, 7:23 AM EDT
if (ThisUser.Company == "My Business Unit")
VulpesRank #55 · 29.2K reputationPosted Aug 12, 2011, 5:25 AM EDT
Max Tenenbaum32 reputationPosted Aug 12, 2011, 6:24 AM EDT
Thanks for your response.
Was wondering if you can help me further, when I try and build the activeDirectoryHelper I get the following errors. See image attahced.
M
VulpesRank #55 · 29.2K reputationPosted Aug 12, 2011, 6:29 AM EDT
Can you try again or explain what the errors are in writing.
Max Tenenbaum32 reputationPosted Aug 12, 2011, 6:31 AM EDT
VulpesRank #55 · 29.2K reputationPosted Aug 12, 2011, 6:56 AM EDT
There should be a class called ADManager in a file called ADManager.cs within the dll project
You may have overlooked it but here's the code which appears in the article:
ADManager.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices.AccountManagement;
namespace ActiveDirectoryHelper
{
public class ADManager
{
PrincipalContext context;
public ADManager()
{
context = new PrincipalContext(ContextType.Machine, "xxx", "xxx", "xxx");
}
public ADManager(string domain, string container)
{
context = new PrincipalContext(ContextType.Domain, domain, container);
}
public ADManager(string domain, string username, string password)
{
context = new PrincipalContext(ContextType.Domain, username, password);
}
public bool AddUserToGroup(string userName, string groupName)
{
bool done = false;
GroupPrincipal group = GroupPrincipal.FindByIdentity(context, groupName);
if (group == null)
{
group = new GroupPrincipal(context, groupName);
}
UserPrincipal user = UserPrincipal.FindByIdentity(context, userName);
if (user != null & group != null)
{
group.Members.Add(user);
group.Save();
done = (user.IsMemberOf(group));
}
return done;
}
public bool RemoveUserFromGroup(string userName, string groupName)
{
bool done = false;
UserPrincipal user = UserPrincipal.FindByIdentity(context, userName);
GroupPrincipal group = GroupPrincipal.FindByIdentity(context, groupName);
if (user != null & group != null)
{
group.Members.Remove(user);
group.Save();
done = !(user.IsMemberOf(group));
}
return done;
}
}
}Max Tenenbaum32 reputationPosted Aug 12, 2011, 7:19 AM EDT
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 15: ActiveDirectoryHelper.ActiveDirectoryHelper adh = new ActiveDirectoryHelper.ActiveDirectoryHelper(); Line 16: ADUserDetail ThisUser = adh.GetUserByLoginName("MYLOGONID"); Line 17: lblWhoAmI.Text = "Hello: " + ThisUser.FirstName.ToString() + "Manager: " + ThisUser.Manager.FirstName + ThisUser.Manager.LastName.ToString(); Line 18: Line 19: }VulpesRank #55 · 29.2K reputationPosted Aug 12, 2011, 7:32 AM EDT
The most obvious one is 'ThisUser'. I take it that you've used your actual log-in ID in place of "MYLOGINID"?
Max Tenenbaum32 reputationPosted Aug 15, 2011, 1:35 AM EDT
Thanks for your help so far as I have now managed to connect to AD and display my details on the aspx page.
However, I need to get the 'GetUserByLoginName' to dynamically recognise a user logged in, so that I can tailor content to their business unit. Any ideas?
Thanks
M
VulpesRank #55 · 29.2K reputationPosted Aug 15, 2011, 6:22 AM EDT
string loginName = Page.User.Identity.Name.Split('\\')[1];
Max Tenenbaum32 reputationPosted Aug 15, 2011, 7:15 AM EDT
Ok the code below worked a treat. I am now able to display the logon details of the user.
protected void Page_Load(object sender, EventArgs e)
{
ActiveDirectoryHelper.ActiveDirectoryHelper adh = new ActiveDirectoryHelper.ActiveDirectoryHelper();
string loginName = Page.User.Identity.Name.Split('\\')[1];
ADUserDetail ThisUser = adh.GetUserByLoginName(loginName);
lblWhoAmI.Text = "Welcome: " + ThisUser.FirstName.ToString() + " " + ThisUser.LastName.ToString() + "
Company: " + ThisUser.Company.ToString();
}
All I need to do now is write a condition statements based on Company. this doesnt seem to work, any idea?
<% if (ThisUser.Company.ToString("My Business Unit"))
{ %>
<% } else { %>
<%} %>
Max Tenenbaum32 reputationPosted Aug 15, 2011, 8:08 AM EDT
Piyush Agrawal10 reputationPosted Sep 12, 2011, 2:49 AM EDT
This text will be replaced by the control CreateControl( "EXAMPLE_DIV_ID", "clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6", "EXAMPLE_OBJECT_ID", "600", "400", "example.wmv", "-1") piyush lepide.comMax Tenenbaum32 reputationPosted Sep 21, 2011, 2:06 AM EDT
Is it possible to add a 2nd AD account/credentials to the App.Config file or any other way?
It turns out that my organisation has 2 AD's so I would like to be able to detect the 2nd one as well within my application.
Code used from http://www.c-sharpcorner.com/UploadFile/dhananjaycoder/activedirectoryoperations11132009113015AM/activedirectoryoperations.aspx
Thanks
VulpesRank #55 · 29.2K reputationPosted Sep 21, 2011, 5:06 AM EDT
Company: " + ThisUser.Company.ToString();
Suthish NairRank #92 · 22.8K reputationPosted Sep 21, 2011, 12:37 PM EDT
Max Tenenbaum32 reputationPosted Sep 22, 2011, 1:13 AM EDT
Thanks for your response, I have followed your instructions and unfortuately I get a null exception error
System.NullReferenceException: Object reference not set to an instance of an object.
Thanks
M
VulpesRank #55 · 29.2K reputationPosted Sep 22, 2011, 1:53 AM EDT
Max Tenenbaum32 reputationPosted Sep 22, 2011, 2:16 AM EDT
I did as you instructed, see changes below
app.config:
ActiveDIrectoryHelper2.cs:
namespace ActiveDirectoryHelper
{
public class ActiveDirectoryHelper2
{
private DirectoryEntry _directoryEntry = null;
private DirectoryEntry SearchRoot
{
get
{
if (_directoryEntry == null)
{
_directoryEntry = new DirectoryEntry(LDAPPath, LDAPUser, LDAPPassword, AuthenticationTypes.Secure);
}
return _directoryEntry;
}
}
private String LDAPPath
{
get
{
return ConfigurationManager.AppSettings["LDAPPath2"];
}
}
private String LDAPUser
{
get
{
return ConfigurationManager.AppSettings["LDAPUser2"];
}
}
private String LDAPPassword
{
get
{
return ConfigurationManager.AppSettings["LDAPPassword2"];
}
}
private String LDAPDomain
{
get
{
return ConfigurationManager.AppSettings["LDAPDomain2"];
}
}
VulpesRank #55 · 29.2K reputationPosted Sep 22, 2011, 5:50 AM EDT
If it can, then my suspicion is that it's not finding the second AD user for some reason.
To test this theory, I'd change the app.config back to what it was except use the credentials for the second user rather than the first.
Also put the Page_Load method back to what it was - you can leave the ActiveDirectoryHelper2 class for now - and log in as the second AD user. If the null exception still occurs, then the problem must lie with that user.
Max Tenenbaum32 reputationPosted Sep 23, 2011, 6:07 AM EDT
I here what your saying but it doesnt work when I substitute loginName for several actual user login names(string). This method worked for the original ActiveDirectoryHelper class, but when trying this method out for ActiveDirectoryHelper2 I get a null exception.
Thanks
M
VulpesRank #55 · 29.2K reputationPosted Sep 23, 2011, 11:05 AM EDT