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
VulpesPosted Aug 15, 2011, 11:23 AM
if (ThisUser.Company == "My Business Unit")
VulpesPosted Sep 23, 2011, 3:05 PM
Max TenenbaumPosted Sep 23, 2011, 10:07 AM
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
VulpesPosted Sep 22, 2011, 9:50 AM
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 TenenbaumPosted Sep 22, 2011, 6:16 AM
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"];
}
}
VulpesPosted Sep 22, 2011, 5:53 AM
Max TenenbaumPosted Sep 22, 2011, 5:13 AM
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
Suthish NairPosted Sep 21, 2011, 4:37 PM
VulpesPosted Sep 21, 2011, 9:06 AM
Company: " + ThisUser.Company.ToString();
Max TenenbaumPosted Sep 21, 2011, 6:06 AM
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
Piyush AgrawalPosted Sep 12, 2011, 6:49 AM
Max TenenbaumPosted Aug 15, 2011, 12:08 PM
Max TenenbaumPosted Aug 15, 2011, 11:15 AM
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 { %>
<%} %>
VulpesPosted Aug 15, 2011, 10:22 AM
string loginName = Page.User.Identity.Name.Split('\\')[1];
Max TenenbaumPosted Aug 15, 2011, 5:35 AM
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
VulpesPosted Aug 12, 2011, 11:32 AM
The most obvious one is 'ThisUser'. I take it that you've used your actual log-in ID in place of "MYLOGINID"?
Max TenenbaumPosted Aug 12, 2011, 11:19 AM
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: }VulpesPosted Aug 12, 2011, 10:56 AM
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 TenenbaumPosted Aug 12, 2011, 10:31 AM
VulpesPosted Aug 12, 2011, 10:29 AM
Can you try again or explain what the errors are in writing.
Max TenenbaumPosted Aug 12, 2011, 10:24 AM
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
VulpesPosted Aug 12, 2011, 9:25 AM