none
PrincipalContext nullreferenceException RRS feed

  • Question

  • My class shown below gets a null domainContext therefore PrincipalContext fails on nullreferenceException.   This happens when I deployed it to Windows 2003 Server Dev or QA box.  The same code works fine in my local.  I appreciate it if some of you can chime in and give me some ideas.

    using System.DirectoryServices.AccountManagement;
    using System.Security.Principal;

        public partial class _Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
            }
            protected void btnAdddonotcall_Click(object sender, EventArgs e)
            {
                string connectionString = WebConfigurationManager.ConnectionStrings["AH1PD01ConnectionString"].ConnectionString;
                string agt = WindowsIdentity.GetCurrent().User.ToString();
                PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, "la.cssd.ad");
                string agtName = UserPrincipal.FindByIdentity(domainContext, agt).Name;
                SqlConnection con = new SqlConnection(connectionString);
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("sp_InsertDoNotCallList", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@phone", SqlDbType.VarChar));
                    cmd.Parameters["@phone"].Value = tbPhone.Text;
                    cmd.Parameters.Add(new SqlParameter("@caller", SqlDbType.VarChar));
                    cmd.Parameters["@caller"].Value = tbCaller.Text;
                    cmd.Parameters.Add(new SqlParameter("@agent", SqlDbType.VarChar));
                    cmd.Parameters["@agent"].Value = agtName;
                    cmd.ExecuteNonQuery();

                }
                catch (Exception ex)
                {
                    Trace.Write(ex.Message);
                }
                finally
                {
                    con.Close();
                }

                Response.Redirect("~/Default.aspx?phone=" + (tbPhone.Text).Trim() + "&caller=" + (tbCaller.Text).Trim());
            }

    Wednesday, September 11, 2013 12:11 AM

Answers

  • Hi Jtc123,

    I agree with Hetro and it is ASP.NET specific case.

    When we run the asp.net application in visual studio on dev box, the asp.net application runs under your current user account. But after we deploy the application into IIS on server, the asp.net application runs under the account configured in iis application pool.

    To solve this issue, you have to change the account of application pool to a domain account which has the permission to perform this action. Or do impersonation.

    Anyway, asp.net forum should be a better way to go forward.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    Alan Yao
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, September 12, 2013 2:29 AM

All replies