Пример #1
0
 public static extern CredUIReturnCodes CredUIPromptForWindowsCredentials(
     ref CREDUI_INFO pUiInfo,
     uint dwAuthError,
     ref uint pulAuthPackage,
     IntPtr pvInAuthBuffer,
     uint ulInAuthBufferSize,
     out IntPtr ppvOutAuthBuffer,
     out uint pulOutAuthBufferSize,
     [MarshalAs(UnmanagedType.Bool)] ref bool pfSave,
     CredUIWinFlags dwFlags);
Пример #2
0
 public static extern uint CredUIPromptForWindowsCredentials(
     ref CREDUI_INFO credInfo,
     int authError,
     ref uint authPackage,
     IntPtr InAuthBuffer,
     uint InAuthBufferSize,
     out IntPtr refOutAuthBuffer,
     out uint refOutAuthBufferSize,
     ref bool fSave,
     CredUIWinFlags flags);
Пример #3
0
 public static extern CredUIReturnCodes CredUIPromptForWindowsCredentials(
     ref CREDUI_INFO pUiInfo,
     uint dwAuthError,
     ref uint pulAuthPackage,
     IntPtr pvInAuthBuffer,
     uint ulInAuthBufferSize,
     out IntPtr ppvOutAuthBuffer,
     out uint pulOutAuthBufferSize,
     [MarshalAs(UnmanagedType.Bool)]ref bool pfSave,
     CredUIWinFlags dwFlags);
Пример #4
0
        public Task <Credentials> GetUserCredentialsAsync(string authority, string workspaceName, CancellationToken cancellationToken = default(CancellationToken))
        {
            _coreShell.AssertIsOnMainThread();

            var credentials = SecurityUtilities.ReadCredentials(authority);

            if (credentials != null)
            {
                return(Task.FromResult(credentials));
            }

            var credui = new CREDUI_INFO {
                cbSize         = Marshal.SizeOf(typeof(CREDUI_INFO)),
                hwndParent     = _coreShell.AppConstants.ApplicationWindowHandle,
                pszCaptionText = Resources.Info_ConnectingTo.FormatInvariant(workspaceName)
            };
            uint           authPkg     = 0;
            IntPtr         credStorage = IntPtr.Zero;
            uint           credSize;
            bool           save  = true;
            CredUIWinFlags flags = CredUIWinFlags.CREDUIWIN_CHECKBOX;
            // For password, use native memory so it can be securely freed.
            IntPtr passwordStorage = SecurityUtilities.CreatePasswordBuffer();
            int    inCredSize      = 1024;
            IntPtr inCredBuffer    = Marshal.AllocCoTaskMem(inCredSize);

            try {
                if (!CredPackAuthenticationBuffer(0, WindowsIdentity.GetCurrent().Name, "", inCredBuffer, ref inCredSize))
                {
                    int error = Marshal.GetLastWin32Error();
                    throw new Win32Exception(error);
                }

                var err = CredUIPromptForWindowsCredentials(ref credui, 0, ref authPkg, inCredBuffer, (uint)inCredSize, out credStorage, out credSize, ref save, flags);
                if (err != 0)
                {
                    throw new OperationCanceledException();
                }

                StringBuilder userNameBuilder = new StringBuilder(CRED_MAX_USERNAME_LENGTH);
                int           userNameLen     = CRED_MAX_USERNAME_LENGTH;
                StringBuilder domainBuilder   = new StringBuilder(CRED_MAX_USERNAME_LENGTH);
                int           domainLen       = CRED_MAX_USERNAME_LENGTH;
                int           passLen         = CREDUI_MAX_PASSWORD_LENGTH;
                if (!CredUnPackAuthenticationBuffer(CRED_PACK_PROTECTED_CREDENTIALS, credStorage, credSize, userNameBuilder, ref userNameLen, domainBuilder, ref domainLen, passwordStorage, ref passLen))
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }

                return(Task.FromResult(Credentials.CreateCredentails(userNameBuilder.ToString(), SecurityUtilities.SecureStringFromNativeBuffer(passwordStorage), save)));
            } finally {
                if (inCredBuffer != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(inCredBuffer);
                }

                if (credStorage != IntPtr.Zero)
                {
                    Marshal.ZeroFreeCoTaskMemUnicode(credStorage);
                }

                if (passwordStorage != IntPtr.Zero)
                {
                    Marshal.ZeroFreeCoTaskMemUnicode(passwordStorage);
                }
            }
        }