Reputation: 39
I am very new to C# and I am getting an issue with a variable that I am not able to call to display on my aspx page.
The code behind is as follow:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
namespace PRIIS
{
public partial class _Default : System.Web.UI.Page
{
private DirectoryEntry GetDirectoryObject()
{
DirectoryEntry oDE;
oDE = new DirectoryEntry("LDAP://192.168.5.4", "user", "xxxx", AuthenticationTypes.Secure);
return oDE;
}
public DirectoryEntry GetUser(string UserName)
{
DirectoryEntry de = GetDirectoryObject();
DirectorySearcher deSearch = new DirectorySearcher();
deSearch.SearchRoot = de;
deSearch.Filter = "(&(objectClass=user)(SAMAccountName=" + UserName + "))";
deSearch.SearchScope = SearchScope.Subtree;
SearchResult results = deSearch.FindOne();
if (!(results == null))
{
de = new DirectoryEntry(results.Path, "user", "xxxx", AuthenticationTypes.Secure);
return de;
}
else
{
return null;
}
}
public class MyUserName
{
public string strName;
public string str;
public void userNameOnSystem()
{
strName = HttpContext.Current.User.Identity.Name.ToString();
string[] splitString = strName.Split('\\');
str = splitString[1];
}
}
protected void Page_Load(object sender, EventArgs e)
{
DirectoryEntry de = GetUser("dramirez");
if (de != null)
{
string displayUser = de.Properties["displayName"].Value.ToString();
Response.Write(displayUser + "<br/>");
//Response.Write(de.Properties["telephoneNumber"].Value.ToString() + "<br/>");
//Response.Write(de.Properties["mail"].Value.ToString() + "<br/>");
//Response.Write(de.Properties["userPrincipalName"].Value.ToString() + "<br/>");
}
}
}
}
And the call that I am doing is to the displayUser string that I want to display on the .aspx page using the following statement
<%= displayUser %>
unfortunately I am getting the error;
Error 2 The name 'displayUser' does not exist in the current context
And I am not sure how or where the issue is coming from.
Thank you in advance for any help.
Upvotes: 0
Views: 1178
Reputation: 68556
Declare your variable as Public.
public string displayUser;
protected void Page_Load(object sender, EventArgs e)
{
DirectoryEntry de = GetUser("dramirez");
if (de != null)
{
displayUser = de.Properties["displayName"].Value.ToString();
Response.Write(displayUser + "<br/>");
//Response.Write(de.Properties["telephoneNumber"].Value.ToString() + "<br/>");
//Response.Write(de.Properties["mail"].Value.ToString() + "<br/>");
//Response.Write(de.Properties["userPrincipalName"].Value.ToString() + "<br/>");
}
}
Upvotes: 0
Reputation: 219096
In order to access the value from within your ASPX markup in that manner, it should be a property in the code-behind (of at least protected
access, I believe). Something like this:
protected string DisplayUser { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
DirectoryEntry de = GetUser("dramirez");
if (de != null)
{
DisplayUser = de.Properties["displayName"].Value.ToString();
}
}
Then it should display with this:
<%= DisplayUser %>
Upvotes: 3