Solved

Reason why this Impersonator code doesn't work?

Posted on 2007-11-20
3
886 Views
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.

Thanks
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, 
                        LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT,
                        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);
                  CloseHandle(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));
            wic.Undo();
            ////
            newItem["ApprovedBy"] = txtApprovedBy.Text;
            newItem["ApprovedDate"] = txtApprovedDate.Text;
            ////
            newItem.Update();

Open in new window

0
Comment
Question by:JordanBlackler
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 11

Accepted Solution

by:
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
0
 

Author Comment

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

Author Comment

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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Scan Files already in SharePoint 2010 on prem ? 5 21
how to remove duplicate code from my project 5 49
Where does legacy ASP.NET initialize language? 2 62
c#, case, if 4 19
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

733 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