Click here to Skip to main content
14,735,271 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I made a web application which get the current user login name search in Active Directory and give the complete information about it. My program runs successfully on localhost but after publishing and deploying it to web server IIS it gives the below error.

Object reference not set to an instance of an object. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace: 


[NullReferenceException: Object reference not set to an instance of an object.]
   MasterPage.Page_Load(Object sender, EventArgs e) +94
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
   System.Web.UI.Control.OnLoad(EventArgs e) +92
   System.Web.UI.Control.LoadRecursive() +54
   System.Web.UI.Control.LoadRecursive() +145
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772
 


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18446 



THE PAGE_LOAD OF MASTER PAGE IS

protected void Page_Load(object sender, EventArgs e)
   {
       ActiveDirectoryHelper obj = new ActiveDirectoryHelper();
       if (Page.IsPostBack == false)
       {
           string other = WindowsIdentity.GetCurrent().Name.ToLower();

           string login = Environment.UserName;
           ADUserDetail person = obj.GetUserByLoginName(login);
           string co = person.Company;
           string name = person.DisplayName;
           string dep = person.Department;


           lblUserName.Text = "WellCome: " +name+dep;
       }
Posted
Updated 3-Jun-14 20:35pm
v2
Comments
ArunRajendra 4-Jun-14 0:57am
   
Can you post active directory related code?

1.Like the error message said, in Page_Load() at line 94 you are trying to access an null object. So maybe your search didn't find any user object and you forgot to manage this case.

2.In order to give you more advises you should provide the code of your Page_Load() method.
   
v2
I am not sure but I guess the issue could be when you are running from iis it will using the ASPNET user. You might need to impersonate use a logged in user account to run the process. Check this link. http://stackoverflow.com/questions/1267071/how-to-get-windows-user-name-when-identity-impersonate-true-in-asp-net[^]
   
if (Page.IsPostBack == false)
{
    string other = WindowsIdentity.GetCurrent().Name.ToLower();
Where is that code running? Whose credentials are used?
Actually that's a really basic issue of web applications:
you have to differentiate between things happening in the browser (client), and thing happening on the server (IIS).
IIS is running with a local account of the server machine, and consequently your code fails.
   
Comments
shaprpuff 9-Jun-14 8:32am
   
I used
identity impersonate="true" username="Domain\loginname" password="xxxxxxxx"> in web.config
with windows authentication =true, anynomus=disable
now there is no error but only the impersonate name is showing on different machine it should display the current login user details. ON localhost it running fine
Instead of using

1st:

WindowsIdentity.GetCurrent().Name.ToLower();


I replace it with the following.
System.Web.HttpContext.Current.User.Identity.Name;



2nd: Give the User Account

<identity impersonate="true" username="Domain\loginname" password="xxxxxxxx"> </identity>

in web.config
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900