impersonating C# code in windows 7
I need to impersonate the my code to run with a different windows user id, right now i am using some code which i got from web which is calling some native libraries.
the purpose of the code is to change the permissions on a file server user directories permissions.
I can change my folder permissions, but i have the credentials of the other user to go and change the permissions on his folder. But i am running the code on my machine by impersonating him.
But, i am getting un authorized exception.
th开发者_StackOverflow社区e code i am using is:
[DllImport("advapi32.dll", SetLastError = true)]
private static extern int LogonUser(
string lpszUserName,
string lpszDomain,
string lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);
I am not sure this is working in Windows 7 or not. Is there any one who faced any issue like this..?
Exception i am getting:
Messagee:"{"Attempted to perform an unauthorized operation."}"
stack trace:
at System.Security.AccessControl.Win32.SetSecurityInfo(ResourceType type, String name, SafeHandle handle, SecurityInfos securityInformation, SecurityIdentifier owner, SecurityIdentifier group, GenericAcl sacl, GenericAcl dacl)
at System.Security.AccessControl.NativeObjectSecurity.Persist(String name, SafeHandle handle, AccessControlSections includeSections, Object exceptionContext)
at System.Security.AccessControl.NativeObjectSecurity.Persist(String name, AccessControlSections includeSections, Object exceptionContext)
at System.Security.AccessControl.NativeObjectSecurity.Persist(String name, AccessControlSections includeSections)
at System.Security.AccessControl.FileSystemSecurity.Persist(String fullPath)
at System.IO.Directory.SetAccessControl(String path, DirectorySecurity directorySecurity)
at FolderAccessManager.Program.Main(String[] args) in
Could you please share some solutions..?
Have a look at WindowsIdentity.Impersonate. There you'll see an example of how to go about.
Are you using a domain joined machine with ASP.NET?
I got the exact same message tring this code in a asp.net application when running with the default apppool identity. Changing the apppool user to a domain user with 'domain admin' permissions solved this problem (also Windows 7).
精彩评论