Link to home
Start Free TrialLog in
Avatar of forsters
forstersFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Query AD from ASP.NET - The Search Filter is invalid (unless I run the page locally)

Hi Experts,

Though I had this nailed, then I published my page.

Not 100% sure what to make of it...
So I'm querying AD from and ASP.NET page to get 4 user details (display name, phone, mobile & job title) and I'm filtering (or trying to filter on user_login id which is held in a session variable.

Code below runs perfectly locally but once I publish the page I get The Search filter is invalid message, can someone explain what the problem is / help me out. Many TIA

CODE BEHIND

using System;
using System.Configuration;
using System.DirectoryServices;


public partial class AD_GK_Default6 : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Security.Principal.IPrincipal user;

        user = System.Web.HttpContext.Current.User;

        System.Security.Principal.IIdentity identity;

        identity = user.Identity;

        Session["username"] = identity.Name.Substring(identity.Name.IndexOf(@"\") + 1);
   

        DirectoryEntry de = new DirectoryEntry(ConfigurationManager.AppSettings.Get("ADPath")); 
        de.Username = ConfigurationManager.AppSettings.Get("ADServiceAccount"); 
        de.Password = ConfigurationManager.AppSettings.Get("ADServiceAccountPassword");
        de.AuthenticationType = AuthenticationTypes.FastBind;

        DirectorySearcher dssearch = new DirectorySearcher(de);

        dssearch.Filter = "(CN=" + Session["username"].ToString() + ")";

        SearchResult sresult = dssearch.FindOne();

        DirectoryEntry dsresult = sresult.GetDirectoryEntry();

        lblfname.Text = dsresult.Properties["displayName"][0].ToString();

        lbltitle.Text = dsresult.Properties["title"][0].ToString();

        lbllname.Text = dsresult.Properties["telephonenumber"][0].ToString();

        lblemail.Text = dsresult.Properties["mobile"][0].ToString();

   }  
}

Open in new window


PAGE CODE
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
   <form id="form1" runat="server">

<div>

<table>

<tr>

<td align="right">



</td>

<td>

<asp:Label ID="lblfname" runat="server" Font-Bold="true"></asp:Label>  

</td>

</tr>

<tr>

<td align="right">



</td>

<td>

<asp:Label ID="lbltitle" runat="server" Font-Bold="true"></asp:Label>  

</td>

</tr>


<tr>

<td align="right">



</td>

<td>

<asp:Label ID="lbllname" runat="server" Font-Bold="true"></asp:Label>  

</td>

</tr>

<tr>

<td align="right">



</td>

<td>

<asp:Label ID="lblemail" runat="server" Font-Bold="true"></asp:Label>  

</td>

</tr>

</table>

</div>

</form>
</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Obadiah Christopher
Obadiah Christopher
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of forsters

ASKER

Ah of course, yes you got me! Thanks so much.