Reason why this Impersonator code doesn't work?

Posted on 2007-11-20
Last Modified: 2008-02-01
Hi All -
I'm using C#

I got this code over a year ago from here. It worked then, but when i added it into my new project it doesn't work. The code doesn't error out, i just get "access denied" when i try to update the list.

public class Impersonator


            public static WindowsIdentity CreateIdentity(string User, string Domain, string Password)


                  // The Windows NT user token.

                  IntPtr tokenHandle = new IntPtr(0);

                  const int LOGON32_PROVIDER_DEFAULT = 0;

                  const int LOGON32_LOGON_NETWORK = 3;

                  tokenHandle = IntPtr.Zero;

                  // Call LogonUser to obtain a handle to an access token.

                  bool returnValue = LogonUser(User, Domain, Password, 


                        ref tokenHandle);

                  if (false == returnValue)


                        int ret = Marshal.GetLastWin32Error();

                        throw new Exception("LogonUser failed with error code: " +  ret);


                  //System.Diagnostics.Debug.WriteLine("Created user token: " + tokenHandle);

                  //The WindowsIdentity class makes a new copy of the token.

                  //It also handles calling CloseHandle for the copy.

                  WindowsIdentity id = new WindowsIdentity(tokenHandle);


                  return id;


            [DllImport("advapi32.dll", SetLastError=true)]

            private static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,

                  int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

            [DllImport("kernel32.dll", CharSet=CharSet.Auto)]

            private extern static bool CloseHandle(IntPtr handle);



protected void btnSaveExit_Click(object sender, EventArgs e)


WindowsImpersonationContext wic = Impersonator.CreateIdentity("Admin User Name", "Domain where Admin resids", "Password").Impersonate();

            SPSite site = SPControl.GetContextSite(Context);

            //site.CatchAccessDeniedException = false;

            SPWeb subSite = site.AllWebs["mysite"];

            subSite.AllowUnsafeUpdates = true;

            SPListCollection allSiteLists = subSite.Lists;

            SPList list = subSite.Lists["mylist"];

            SPListItemCollection listItems = list.Items;

            SPListItem newItem = list.GetItemById(Convert.ToInt32(lblUnique.Text));



            newItem["ApprovedBy"] = txtApprovedBy.Text;

            newItem["ApprovedDate"] = txtApprovedDate.Text;



Open in new window

Question by:JordanBlackler
  • 2
LVL 11

Accepted Solution

Salim Fayad earned 500 total points
ID: 20321028
Make sure that the user has access to the Sharepoint site and that he has write access on the list

Author Comment

ID: 20321401
If they have write access, then wouldn't they people able to edit the list anyways?

Author Comment

ID: 20322631
I did what your suggested and i also changed the way i got the site and list. Now it works.

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now