Autor Beitrag
A3eXy
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 25



BeitragVerfasst: Fr 14.05.10 10:42 
Hallo Zusammen,

Ich habe wiedermal ein Problem und zwar mit der Impersonation.
Die Impersonation selber funktioniert bestens, nur wenn ich mit dem Impersonation User etwas löschen will macht er es nicht.

Wenn ich das Programm von Anfang an mit "Ausführen als" ausführe, dann funktionierts.

Gibt es irgendeinen Parameter den ich noch zusätzlich hinzufügen kann, dass er mehr Rechte hat?
Hab leider im Netz nicht wirklich was gefunden...

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
using System.Runtime.InteropServices;  // DllImport
using System.Security.Principal; // WindowsImpersonationContext
using System.Security.Permissions; // PermissionSetAttribute      

        // obtains user token
        [DllImport("advapi32.dll", SetLastError = true)]
        public static extern bool LogonUser(string pszUsername, string pszDomain, string pszPassword,
            int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

        // closes open handes returned by LogonUser
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public extern static bool CloseHandle(IntPtr handle);

        // creates duplicate token handle
        [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public extern static bool DuplicateToken(IntPtr ExistingTokenHandle,
            int SECURITY_IMPERSONATION_LEVEL, ref IntPtr DuplicateTokenHandle);

        private System.Security.Principal.WindowsImpersonationContext someToken;

        [PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
        public WindowsImpersonationContext Impersonate()
        {
            IntPtr tokenHandle = new IntPtr(0);
            IntPtr dupeTokenHandle = new IntPtr(0);
            try
            {
                string domainName;
                domainName = "local";             

                const int LOGON32_PROVIDER_DEFAULT = 0;
                //This parameter causes LogonUser to create a primary token.
                const int LOGON32_LOGON_INTERACTIVE = 2;

                tokenHandle = IntPtr.Zero;

                // Call LogonUser to obtain a handle to an access token.
                bool returnValue = LogonUser(NewWinUser, domainName, NewWinPWD,
                    LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
                    ref tokenHandle);

                if (false == returnValue)
                {
                    int ret = Marshal.GetLastWin32Error();
                    throw new System.ComponentModel.Win32Exception(ret);
                }

                // Use the token handle returned by LogonUser.
                WindowsIdentity newId = new WindowsIdentity(tokenHandle);
                WindowsImpersonationContext impersonatedUser = newId.Impersonate();
                //MessageBox.Show(WindowsIdentity.GetCurrent().Name);
                return impersonatedUser;
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Application.Exit();
                return null;
            }
        }