private static extern int InitializeSecurityContext( ref SECURITY_HANDLE phCredential, //PCredHandle IntPtr phContext, //PCtxtHandle string pszTargetName, int fContextReq, int Reserved1, int TargetDataRep, IntPtr pInput, //PSecBufferDesc SecBufferDesc int Reserved2, out SECURITY_HANDLE phNewContext, //PCtxtHandle out SecBufferDesc pOutput, //PSecBufferDesc SecBufferDesc out uint pfContextAttr, //managed ulong == 64 bits!!! out SECURITY_INTEGER ptsExpiry); //PTimeStamp
private static string getToken() { SECURITY_HANDLE hCredential = new SECURITY_HANDLE(); SECURITY_INTEGER tsExpiry = new SECURITY_INTEGER(); IntPtr pAuthIdentity = IntPtr.Zero; // The structure for storing user data entered int stat = AcquireCredentialsHandle( null, "Negotiate", SECPKG_CRED_OUTBOUND, IntPtr.Zero, pAuthIdentity, 0, IntPtr.Zero, ref hCredential, ref tsExpiry); if (stat != SEC_E_OK) { return(""); } //-------------------------------------------------------------------- SECURITY_HANDLE m_hCtxt; SecBufferDesc SecBufDesc = new SecBufferDesc(MAX_TOKEN_SIZE); uint fContextAttr; stat = InitializeSecurityContext( ref hCredential, IntPtr.Zero, targetName, ISC_REQ_CONFIDENTIALITY, 0, // reserved1 SECURITY_NATIVE_DREP, IntPtr.Zero, 0, // reserved2 out m_hCtxt, out SecBufDesc, out fContextAttr, out tsExpiry); if (stat != SEC_E_OK && stat != SEC_I_CONTINUE_NEEDED) { return(""); } string token = Convert.ToBase64String(SecBufDesc.GetSecBufferByteArray()); if (token.Length < 500) { CREDUI_INFO creduiInfo = new CREDUI_INFO(); creduiInfo.cbSize = Marshal.SizeOf(creduiInfo); creduiInfo.pszMessageText = "Введите имя пользователя и пароль для подключения к " + hostName; creduiInfo.pszCaptionText = "Подключение к прокси-серверу"; //creduiInfo.hwndParent = Form.ActiveForm.Handle; bool fSave = true; IntPtr ppAutchIdent = Marshal.AllocHGlobal(1024); stat = SspiPromptForCredentials( targetName, ref creduiInfo, 0, "Negotiate", IntPtr.Zero, ppAutchIdent, ref fSave, 0); Marshal.FreeHGlobal(ppAutchIdent); if (stat != SEC_E_OK) { return(""); } } SecBufDesc.Dispose(); return(token); }