c# IDsObjectPickerCredentials fail

本文关键字:fail IDsObjectPickerCredentials | 更新日期: 2023-09-27 17:49:17

我必须在c#上设置凭据为Active-Directory-Object-Picker (IDsObjectPicker)。但是我不能强制IDsObjectPickerCredentials工作。我在c++上做了同样的(msdn示例),它工作得很好。

我使用了"headers"从这里ComInterop.cs和StructsFlags.cs

请告诉我我做错了什么。如何调用IDsObjectPickerCredentials。SetCredentials

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Tulpep.ActiveDirectoryObjectPicker;
namespace cred
{
    class Program
    {
        static void Main(string[] args)
        {
            string szTargetComputer = @"10.0.9.115";
            string szUser = @"TST'test"; 
            string szPassword = @"123qazWSX";
            DSObjectPicker picker = new DSObjectPicker();
            IDsObjectPicker ipicker = (IDsObjectPicker)picker;
            int res = InitObjectPicker(ipicker, szTargetComputer);
            if (res == (int)HRESULT.S_OK)
            {
                try
                {
                    // !!! HERE !!!
                    IDsObjectPickerCredentials cred = (ipicker as IDsObjectPickerCredentials);
                    res = cred.SetCredentials(szUser, szPassword); 
                    // c++ like variant
                    // res = InitCredentials(ipicker, szUser, szPassword);
                    if (res != (int)HRESULT.S_OK) Console.WriteLine("SetCredentials Fail : 0x{0}", res.ToString("X4")); // On Win32 I get 0x80070057 - looks like E_INVALIDARG
                    IntPtr hwndParent = Process.GetCurrentProcess().MainWindowHandle;
                    IDataObject dataObj = null;
                    int hresult = ipicker.InvokeDialog(hwndParent, out dataObj);
                    Console.WriteLine(hresult == (int)HRESULT.S_OK ? "OK" : "Cancel");
                    Console.ReadKey();
                }
                finally
                {
                    Marshal.ReleaseComObject(ipicker);
                }
            }
        }

        [ComImport, Guid("E2D3EC9B-D041-445A-8F16-4748DE8FB1CF"),
            InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        internal interface IDsObjectPickerCredentials
        {
            [PreserveSig()]
            int SetCredentials(
                [In, MarshalAs(UnmanagedType.LPWStr)] string szUserName,
                [In, MarshalAs(UnmanagedType.LPWStr)] string szPassword);
        }
        static int InitObjectPicker(IDsObjectPicker ipicker, string szTargetComputer)
        {          
            int res = (int)HRESULT.S_FALSE;
            DSOP_SCOPE_INIT_INFO[] aScopeInit = new DSOP_SCOPE_INIT_INFO[1];
            DSOP_INIT_INFO InitInfo = new DSOP_INIT_INFO();
            aScopeInit[0].cbSize = (uint)Marshal.SizeOf(typeof(DSOP_SCOPE_INIT_INFO));
            aScopeInit[0].flType = 
                DSOP_SCOPE_TYPE_FLAGS.DSOP_SCOPE_TYPE_UPLEVEL_JOINED_DOMAIN | 
                DSOP_SCOPE_TYPE_FLAGS.DSOP_SCOPE_TYPE_DOWNLEVEL_JOINED_DOMAIN;
            aScopeInit[0].FilterFlags.Uplevel.flBothModes = 
                DSOP_FILTER_FLAGS_FLAGS.DSOP_FILTER_COMPUTERS |
                DSOP_FILTER_FLAGS_FLAGS.DSOP_FILTER_USERS |
                DSOP_FILTER_FLAGS_FLAGS.DSOP_FILTER_WELL_KNOWN_PRINCIPALS |
                DSOP_FILTER_FLAGS_FLAGS.DSOP_FILTER_BUILTIN_GROUPS |
                DSOP_FILTER_FLAGS_FLAGS.DSOP_FILTER_WELL_KNOWN_PRINCIPALS |
                DSOP_FILTER_FLAGS_FLAGS.DSOP_FILTER_INCLUDE_ADVANCED_VIEW;
            aScopeInit[0].FilterFlags.flDownlevel =
                DSOP_FILTER_FLAGS_FLAGS.DSOP_FILTER_COMPUTERS |
                DSOP_FILTER_FLAGS_FLAGS.DSOP_FILTER_USERS |
                DSOP_FILTER_FLAGS_FLAGS.DSOP_FILTER_WELL_KNOWN_PRINCIPALS |
                DSOP_FILTER_FLAGS_FLAGS.DSOP_FILTER_BUILTIN_GROUPS |
                DSOP_FILTER_FLAGS_FLAGS.DSOP_FILTER_WELL_KNOWN_PRINCIPALS |
                DSOP_FILTER_FLAGS_FLAGS.DSOP_FILTER_INCLUDE_ADVANCED_VIEW;
            IntPtr refScopeInitInfo = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(DSOP_SCOPE_INIT_INFO)) * aScopeInit.Length);
            try
            {
                // Marshal structs to pointers
                for (int index = 0; index < aScopeInit.Length; index++)
                {
                    Marshal.StructureToPtr(aScopeInit[index], (IntPtr)((int)refScopeInitInfo + index * Marshal.SizeOf(typeof(DSOP_SCOPE_INIT_INFO))), false);
                }
                InitInfo.cbSize = (uint)Marshal.SizeOf(typeof(DSOP_INIT_INFO));
                InitInfo.cDsScopeInfos = (uint)aScopeInit.Length; //sizeof(aScopeInit)/sizeof(DSOP_SCOPE_INIT_INFO);
                InitInfo.aDsScopeInfos = refScopeInitInfo;
                InitInfo.flOptions = DSOP_INIT_INFO_FLAGS.DSOP_FLAG_MULTISELECT;
                InitInfo.pwzTargetComputer = szTargetComputer;
                res = ipicker.Initialize(ref InitInfo);
            }
            finally
            {
                Marshal.FreeHGlobal(refScopeInitInfo);
            }
            return res;
        }
        static int InitCredentials(IDsObjectPicker ipicker, string User, string Password)
        {
            IntPtr ptr = IntPtr.Zero;
            Guid IID_IDsObjectPickerCredentials = new Guid("E2D3EC9B-D041-445A-8F16-4748DE8FB1CF");
            IntPtr pIUnk = Marshal.GetIUnknownForObject(ipicker);           
            int hr = Marshal.QueryInterface(pIUnk, ref IID_IDsObjectPickerCredentials, out ptr);
            if (hr == HRESULT.S_OK)
            {
                try
                {
                    IDsObjectPickerCredentials cred = (IDsObjectPickerCredentials)Marshal.GetObjectForIUnknown(ptr);
                    hr = cred.SetCredentials(User, Password);
                    if (hr != HRESULT.S_OK)
                    {
                        System.Diagnostics.Debugger.Break(); // Fail
                        return (int)HRESULT.S_FALSE;
                    }
                }
                catch (Exception ex)
                {
                    return (int)HRESULT.S_FALSE;
                }
                finally
                {
                    Marshal.Release(ptr);
                }
            }
            return (int)HRESULT.S_OK;
        }
    }
}

c# IDsObjectPickerCredentials fail

IDsObjectPickerCredentials接口来源于IDsObjectPicker。您的示例实际上使用用户名和密码而不是SetCredentials调用Initialize方法。

下面是一个正确的声明:
[ComImport, Guid("e2d3ec9b-d041-445a-8f16-4748de8fb1cf"), 
 InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IDsObjectPickerCredentials
{
    [PreserveSig]
    int Initialize(ref DSOP_INIT_INFO pInitInfo);
    [PreserveSig]
    int InvokeDialog(IntPtr HWND, out IDataObject lpDataObject);
    [PreserveSig]
    int SetCredentials(string userName, string password);
}

请注意,代码不是从IDsObjectPicker派生,而是以相同的顺序复制其方法。这是。COM互操作所必需的。

你不需要手动调用QueryInterface/Release, var cred = (IDsObjectPickerCredentials)iobjectpicker;就足够了。

我还发现SetCredentials应该在Initialize方法之前调用。