English 中文(简体)
How do you add user account policies programmatically?
原标题:

I was using the APIs NetUserAdd and NetUserSetGroups to add a user account and associate it with a group to a computer. Not much later I found an example capable of doing the same thing (adding a user, associating him with a group) using System.DirectoryServices namespace and DirectoryEntry object.

Now I need to add policies to that new account and am faced with a similar problem. I can use the LsaOpenPolicy and LsaAddAccountRights APIs to add a policy to and account, but I d prefer to use .NET objects and methods.

My question, is/are there object methods in the .NET framework to add and/or modify user account policy settings, and if so what are the names of those objects or methods?

Also an example of usage would be appreciated.

Specifically, I am looking to add the "SeServiceLogonRight", otherwise known as the log in as a service right, to a new user account.

问题回答

No, I do not believe any of the current .net Assemblies implement those methods. These are the only methods I found in the Fx 2.0, 3.0, and 3.5 assemblies with a DllImportAttribute, and a name /Lsa.+/

[mscorlib] Microsoft.Win32.Win32Native.LsaNtStatusToWinError Microsoft.Win32.Win32Native.LsaDeregisterLogonProcess Microsoft.Win32.Win32Native.LsaClose Microsoft.Win32.Win32Native.LsaFreeReturnBuffer Microsoft.Win32.Win32Native.LsaLookupSids Microsoft.Win32.Win32Native.LsaFreeMemory Microsoft.Win32.Win32Native.LsaLookupNames Microsoft.Win32.Win32Native.LsaOpenPolicy Microsoft.Win32.Win32Native.LsaLogonUser Microsoft.Win32.Win32Native.LsaLookupAuthenticationPackage Microsoft.Win32.Win32Native.LsaRegisterLogonProcess Microsoft.Win32.Win32Native.LsaLookupNames2 Microsoft.Win32.Win32Native.LsaConnectUntrusted
Microsoft.Win32.Win32Native.LsaGetLogonSessionData

That said, someone wrote a managed wrapper on these functions. http://www.hightechtalks.com/csharp/lsa-functions-276626.html

There is also a great article on Codeproject, LSA Functions - Privileges and Impersonation http://www.codeproject.com/KB/cs/lsadotnet.aspx





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签