IDsObjectPickerCredentials

IDsObjectPickerCredentials

我必须在c#上将凭据设置为Active-Directory-Object-Picker(IDsObjectPicker)。
但是我不能强迫IDsObjectPickerCredentials工作。
我在c ++(msdn example)上取得了相同的效果,并且效果很好。

我从这里ComInterop.cs and 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;
        }
    }
}

最佳答案

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

这是正确的声明:

[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派生,而是以相同顺序复制其方法。这对于.NET COM Interop是必需的。

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

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

关于c# - C#IDsObjectPickerCredentials失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38037523/

10-12 18:04