2

I am receiving the classic, Object reference not set to an instance of an object in my project when viewing the hosted website. Works when building a debug version locally.

Live

Example of code that is showing error message:

     using System.DirectoryServices.AccountManagement;
     protected void Page_Load(object sender, EventArgs e)
            {
                try
                {

                    String username = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
                    username = username.Substring(3);
                    PrincipalContext pc = new PrincipalContext(ContextType.Domain, "dc");
                    UserPrincipal user = UserPrincipal.FindByIdentity(pc, username);
                    string NTDisplayName = user.DisplayName;
                    //String NTUser = user.SamAccountName;
                    lblntuser.Text = NTDisplayName;

                }
                catch (Exception Ex)
                {
                    lblntuser.Text = Ex.Message;
                    System.Diagnostics.Debug.Write(Ex.Message);
                }
            }
Rafael
  • 3,081
  • 6
  • 32
  • 53
JS1986
  • 1,920
  • 4
  • 29
  • 50
  • 3
    on which line? step through with debugger... – Mitch Wheat Oct 29 '12 at 04:27
  • 1
    Are you sure that WindowsIdentity.GetCurrent() returns a non-null value? – Fyodor Soikin Oct 29 '12 at 04:28
  • @FyodorSoikin nail on the head. It's using NT AUTHORITY\NETWORK SERVICE of my webserver as oppposed to the user that is browsing the page. – JS1986 Oct 29 '12 at 04:34
  • This call `UserPrincipal user = UserPrincipal.FindByIdentity(pc, username);` **can** fail - you need to check `if (user != null) ...` before using that instance! – marc_s Oct 29 '12 at 05:53
  • A good reason for not having code that swallows useful pieces of error information - you're unable to inspect e.g. the StackTrace of any exception. It's far better to let exceptions escape and hit general error handlers. – Damien_The_Unbeliever Oct 29 '12 at 15:54

1 Answers1

1

Try this:

protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        // you need to also take into account that someone could get to your
        // page without having a Windows account.... check for NULL !
        if (System.Security.Principal.WindowsIdentity == null ||
            System.Security.Principal.WindowsIdentity.GetCurrent() == null)
        {
            return;  // possibly return a message or something....
        }

        String username = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

        // if the user name returned is null or empty -> abort
        if(string.IsNullOrEmpty(username))
        {
           return;
        }

        username = username.Substring(3);

        PrincipalContext pc = new PrincipalContext(ContextType.Domain, "dc");

        UserPrincipal user = UserPrincipal.FindByIdentity(pc, username);

        // finding the user of course can also fail - check for NULL !!
        if (user != null)
        {
            string NTDisplayName = user.DisplayName;
            //String NTUser = user.SamAccountName;
            lblntuser.Text = NTDisplayName;
        }
     }
     catch (Exception Ex)
     {
        lblntuser.Text = Ex.Message;
        System.Diagnostics.Debug.Write(Ex.Message);
     }
 }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459