using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; using Vanara.Extensions; using Vanara.InteropServices; using static Vanara.PInvoke.AdvApi32; namespace Vanara.PInvoke { /// Functions, enumerations and structures found in NetSecApi.dll. public static partial class NetSecApi { /// The MSV1_0 authentication package name. [PInvokeData("Ntsecapi.h")] public const string MSV1_0_PACKAGE_NAME = "MICROSOFT_AUTHENTICATION_PACKAGE_V1_0"; /// The Kerberos authentication package name. [PInvokeData("Ntsecapi.h")] public const string MICROSOFT_KERBEROS_NAME = "Kerberos"; /// The Negotiate authentication package name. [PInvokeData("Security.h")] public const string NEGOSSP_NAME = "Negotiate"; /// The LsaLookupAuthenticationPackage function obtains the unique identifier of an authentication package. /// Handle obtained from a previous call to LsaRegisterLogonProcess or LsaConnectUntrusted. /// A string that specifies the name of the authentication package. The package name must not exceed 127 bytes in length. The following table lists the names of the Microsoft-provided authentication packages. /// /// ValueMeaning /// MSV1_0_PACKAGE_NAMEThe MSV1_0 authentication package name. /// MICROSOFT_KERBEROS_NAMEThe Kerberos authentication package name. /// NEGOSSP_NAMEThe Negotiate authentication package name. /// /// /// Pointer to a ULONG that receives the authentication package identifier. /// If the function succeeds, the return value is STATUS_SUCCESS. /// If the function fails, the return value is an NTSTATUS code. The following are possible error codes. /// /// Return codeDescription /// STATUS_NO_SUCH_PACKAGEThe specified authentication package is unknown to the LSA. /// STATUS_NAME_TOO_LONGThe authentication package name exceeds 127 bytes. /// [DllImport(Lib.Secur32, ExactSpelling = true)] [PInvokeData("Ntsecapi.h", MSDNShortId = "aa378297")] public static extern uint LsaLookupAuthenticationPackage(SafeLsaConnectionHandle LsaHandle, [In, MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LsaStringMarshaler))] string PackageName, out int AuthenticationPackage); /// The LsaConnectUntrusted function establishes an untrusted connection to the LSA server. /// Pointer to a handle that receives the connection handle, which must be provided in future authentication services. /// If the function succeeds, the return value is STATUS_SUCCESS. /// If the function fails, the return value is an NTSTATUS code. For more information, see LSA Policy Function Return Values. /// The LsaNtStatusToWinError function converts an NTSTATUS code to a Windows error code. [DllImport(Lib.Secur32, ExactSpelling = true)] [PInvokeData("Ntsecapi.h", MSDNShortId = "aa378265")] public static extern uint LsaConnectUntrusted(out SafeLsaConnectionHandle LsaHandle); /// The LsaDeregisterLogonProcess function deletes the caller's logon application context and closes the connection to the LSA server. /// Handle obtained from a LsaRegisterLogonProcess or LsaConnectUntrusted call. /// If the function succeeds, the return value is STATUS_SUCCESS. /// If the function fails, the return value is an NTSTATUS code. For more information, see LSA Policy Function Return Values. /// The LsaNtStatusToWinError function converts an NTSTATUS code to a Windows error code. [DllImport(Lib.Secur32, ExactSpelling = true)] [PInvokeData("Ntsecapi.h", MSDNShortId = "aa378269")] public static extern uint LsaDeregisterLogonProcess(IntPtr LsaHandle); /// The LsaRegisterLogonProcess function establishes a connection to the LSA server and verifies that the caller is a logon application. /// String identifying the logon application. This should be a printable name suitable for display to administrators. For example, the Windows logon application might use the name "User32LogonProcess". This name is used by the LSA during auditing. LsaRegisterLogonProcess does not check whether the name is already in use. This string must not exceed 127 bytes. /// Pointer that receives a handle used in future authentication function calls. /// The value returned is not meaningful and should be ignored. /// If the function succeeds, the return value is STATUS_SUCCESS. /// If the function fails, the return value is an NTSTATUS code. For more information, see LSA Policy Function Return Values. /// The LsaNtStatusToWinError function converts an NTSTATUS code to a Windows error code. [DllImport(Lib.Secur32, ExactSpelling = true)] [PInvokeData("Ntsecapi.h", MSDNShortId = "aa378318")] public static extern uint LsaRegisterLogonProcess([In, MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LsaStringMarshaler))] string LogonProcessName, out SafeLsaConnectionHandle LsaHandle, out uint SecurityMode); /// /// A SafeHandle for security descriptors. If owned, will call LocalFree on the pointer when disposed. /// [PInvokeData("Ntsecapi.h")] public class SafeLsaConnectionHandle : GenericSafeHandle { /// Initializes a new instance of the class. public SafeLsaConnectionHandle() : this(IntPtr.Zero) {} /// Initializes a new instance of the class from an existing pointer. /// The connection handle. /// if set to true indicates that this pointer should be freed when disposed. public SafeLsaConnectionHandle(IntPtr handle, bool own = true) : base(handle, h => LsaDeregisterLogonProcess(h) == 0, own) { } } } }