C# .NET - not getting active directory user details

Asked By jidheesh Rajan on 06-Dec-12 05:08 AM


Hi,

I am not getting the Active directory user details after hostig my website in local iis (varsion5.1).  Can anyone help me??


thanks
Jidheesh
Robbe Morris replied to jidheesh Rajan on 06-Dec-12 09:33 AM
You won't unless you set the IIS application NOT to use anonymous authentication.  It must use windows authentication only.
jidheesh Rajan replied to Robbe Morris on 07-Dec-12 08:18 AM



I did what u suggested. The details i am getting from AD. But I need to enter the user name and password. Is there any option to get the details with out entering the password. So that i can avoid my login screen.
Robbe Morris replied to jidheesh Rajan on 07-Dec-12 08:20 AM
jidheesh Rajan replied to Robbe Morris on 09-Dec-12 11:35 PM

Hi, Thank you so much for supporting me...

I referred the link , in the function
 public static bool LoadSession(string userName, string password, string domainControllerServerName)

you are passing the password. But I don't want to pass the password to AD. I need to  get the Currently logged in  Active directory user details with out entering the password. Could you please have a look on this and help me?

Thanks in advance
jidhu  
Robbe Morris replied to jidheesh Rajan on 10-Dec-12 08:28 AM
Read closer.  I just used it to validate their credentials.  You do not need to do that.  You need the code below it.
jidheesh Rajan replied to Robbe Morris on 11-Dec-12 08:04 AM
I tried that code. its working fine in Visual studio development server. But after hosting to IIS, the user name is displaying as
ASPNET and not getting any other AD Details. I am using IIS5.1 . Please have a look on this and help me.
Robbe Morris replied to jidheesh Rajan on 11-Dec-12 08:04 AM
Are you hard coding the user's username or are you attempting to get it from the Request.ServerVariables collection?
jidheesh Rajan replied to Robbe Morris on 12-Dec-12 01:20 AM


Thank you very much for your support.

I am getting the username with this code.

private String GetLoggedUser()
        {
            string Username = Page.User.Identity.Name;
            int hasDomain = Username.IndexOf(@"\");
            if (hasDomain > 0)
            {
                Username = Username.Remove(0, hasDomain + 1);
            }
            return Username;
        }


And i am trying to get the AD Details with following code

string connection = "LDAP://SEDC.HELLO.COM";
            String domainAndUsername = "HELLO" + @"\" + GetLoggedUser();
          using (DirectoryEntry entry = new DirectoryEntry(connection))
            {
                string UserName = GetLoggedUser();
                try
                {
                    DirectorySearcher dssearch = new DirectorySearcher(entry);
                    dssearch.Filter = "(sAMAccountName=" + UserName + ")";
                    dssearch.PropertiesToLoad.Add("cn");
                    SearchResult sresult = dssearch.FindOne();
                    string path = sresult.Path;
                    DirectoryEntry dsresult = sresult.GetDirectoryEntry();
                    if (dsresult.Properties["givenName"].Value != null)
                        txtFullName.Text = dsresult.Properties["givenName"][0].ToString();
                    //if (dsresult.Properties["sn"].Value != null)
                    //    txtTelephone.Text = dsresult.Properties["sn"][0].ToString();
                    if (dsresult.Properties["mail"].Value != null)
                        txtEmail.Text = dsresult.Properties["mail"][0].ToString();
                    if (dsresult.Properties["telephoneNumber"].Value != null)
                        txtTelephone.Text = dsresult.Properties["telephoneNumber"][0].ToString();
                }
                catch (Exception ex)
                {
                    lblError.Text = ex.Message.ToString();
                }
            }


After hosting this, username is coming  ASPNET.  and able to connect AD.

Please have a look on this and help me..
Thanks in advance
Jidhu
Robbe Morris replied to jidheesh Rajan on 12-Dec-12 08:31 AM
I already gave you the code but yet you are not using it.  I can't help you if you won't help yourself.
help
I created a small program that should be able to validate using System.Linq; using System.Text; using System.DirectoryServices.AccountManagement; namespace TryAccount { class Program { static PrincipalContext m_principalContext; static UserPrincipal m_userPrincipal; public static bool AuthenticateUser(string username, string password) { bool isAuthentic = false TODO: add support for user@domain format { / / string[] holdName = username.Split(new char[] { ' \ ' }); m_principalContext = new PrincipalContext(ContextType.Domain); isAuthentic = m_principalContext.ValidateCredentials(username, password, ContextOptions.Negotiate); } else { m_principalContext = new PrincipalContext(ContextType.Machine); isAuthentic = m_principalContext.ValidateCredentials(username, password, ContextOptions.Negotiate); } if (isAuthentic) { m_userPrincipal = UserPrincipal.FindByIdentity(m_principalContext, username); Console.WriteLine("User " + m_userPrincipal.Name + Console.WriteLine("Last logged on " + m_userPrincipal.LastLogon
Account(account); if (!principalContexts.ContainsKey(a.Domain)) { return ErrorMsg.Invalid_Account; } PrincipalContextStore pcs = principalContexts[a.Domain]; PrincipalContext ctx = pcs.Context; if (ctx = = null) { return ErrorMsg.Invalid_Account; } UserPrincipal p = UserPrincipal.FindByIdentity(ctx, a.UserName); if (p ! = null) { return ErrorMsg.Duplicate_Account; } Console.WriteLine(ctx.ValidateCredentials(pcs.OperatorAccount.UserName, pcs.OperatorPassword)); p = new UserPrincipal(ctx, a.UserName, password, true); Console.WriteLine ctx.Options.ToString(), ctx.ConnectedServer, ctx.Name, ctx.UserName)); p.Save(); return ErrorMsg.None; The PrincipalContext user credential is right, some AD read operations are working fine (such as PrincipalContext.ValidateCredentials()), and the user can create the AD users in Windows computer management normally. Please kindly advise. Thanks! ADSI Discussions ActiveDirectorySchema.GetSchema (1) UserPrincipal.FindByIdentity (1) PrincipalContext.ValidateCredentials (1) DirectoryContextType.Forest (1) SchemaClassType.Structural (1) ErrorMsg.Duplicate (1) Console
am using this code to get groups with the other domain OK just not with the GetAuthorizationGroups() function. ArrayList results = new ArrayList(); PrincipalContext context = new PrincipalContext(ContextType.Domain, null, _DomainDN, _UserName, _Password); UserPrincipal p = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, username); var groups = p.GetAuthorizationGroups(); foreach (GroupPrincipal group in groups) { results.Add the authorization groups, an error (5) occurred. Source Error: Line 281: { Line 282: UserPrincipal p = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, username); Line 283: var groups = p.GetAuthorizationGroups(); Line 284: / / var groups = p ADRoleProvider.GetRolesForUser (1) System.Web.UI.Page.RaiseChangedEvents (1) System.Web.UI.Page.ProcessRequestMain (1) UserPrincipal.FindByIdentity (1) IdentityType.SamAccountName (1) UserGroup.LoadUsersGroups (1) Active Directory (1) UserMaintenance.LoadUser (1) Are
string userName, string password, string domainControllerServerName) { var activeDirectoryGroups = new List < string > (); using ( var pc = new PrincipalContext ( ContextType . Domain , domainControllerServerName)) { var validated = pc. ValidateCredentials (userName. Trim (), password. Trim ()); if (!validated) return false ; var user = UserPrincipal . FindByIdentity (pc, IdentityType . SamAccountName , userName); if (user = = null) return false; / / get p roperties from active directory
bool BindUser(string userName, string domainName, string password) { bool retVal = false; try { UserPrincipal userPrincipal = null; PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, domainName); userPrincipal = UserPrincipal.FindByIdentity(principalContext, userName); DirectoryEntry de = new DirectoryEntry("LDAP: / / " + principalContext.ConnectedServer + " / " + userPrincipal.DistinguishedName, userName, password); string temp = de.Name; retVal = true; } catch (Exception ex) { } return me how to bind the user in child domain from the parent domain. ADSI Discussions UserPrincipal.FindByIdentity (1) ContextType.Domain (1) PrincipalContext.ConnectedServer (1) UserPrincipal.DistinguishedName (1) PrincipalContext (1) DirectoryEntry (1
how? } Thanks in advance! Nikolay C# Discussions System.DirectoryServices.AccountManagement (1) System.DirectoryServices.ActiveDirectory (1) UserPrincipal.FindByIdentity (1) WindowsIdentity.GetCurrent (1) System.DirectoryServices (1) DirectorySearcher (1) PrincipalContext (1) WindowsIdentity (1) It depends on what Windows Account you are talking about (local, domain replies. I guess I find out how could I Create or modify user in domain: PrincipalContext pc = new PrincipalContext(System.DirectoryServices.AccountManagement.ContextType.Domain, "your domain"); UserPrincipal up = UserPrincipal.FindByIdentity(pc, IdentityType.Sid, WindowsIdentity.GetCurrent().User.Value); That's regarding the currunt user; if you
I'm trying to use IIS-supplied credentials to connect to AD DS the browser is running on another machine, however, it throws this exception after calling the PrincipalContext ctor: COMException (0x80072020): An operations error occurred. (I get the same results if the impersonation WindowsImpersonationContext aspContext = null; WindowsIdentity userIdentity = (WindowsIdentity)User.Identity; UserName = userIdentity.Name; try { aspContext = userIdentity.Impersonate(); PrincipalContext domainContext = new PrincipalContext(ContextType.Domain); UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(domainContext, IdentityType.Sid, userIdentity.User.ToString()); EmailAddress = userPrincipal.EmailAddress; } catch { } finally { if (aspContext ! = null) aspContext.Undo(); } } . . . } - - Rich Armstrong ADSI Discussions UserPrincipal FindByIdentity (1) WindowsIdentity.Impersonate (1) ContextType.Domain (1) App Pool (1) ASP.NET (1) IIS (1
operation on the RDN attribute of an object. (Exception from HRESULT: 0x80072016)"} Code: UserPrincipal myUserPrincipal = UserPrincipal.FindByIdentity(CreateContext(), userName); myUserPrincipal.Name = "newName"; I can change the GiveName, SamAccountName and UserPrincipalName w / o this is by design, but just in case I'm doing something wrong. . . C# Discussions UserPrincipal.FindByIdentity (1) FindByIdentity (1) UserPrincipal (1) CreateContext (1) UserPrincipalName (1) SamAccountName (1) HRESULT (1) MyUserPrincipal (1