簡體   English   中英

c#IDsObjectPickerCredentials失敗

[英]c# IDsObjectPickerCredentials fail

我必須在c# 上將憑據設置為Active-Directory-Object-Picker( IDsObjectPicker )。 但我無法強制使用IDsObjectPickerCredentials 我在c ++( msdn示例 )上做了同樣的工作並且效果很好。

我使用了ComInterop.cs和StructsFlags.cs中的 “headers”

請告訴我我做錯了什么。 如何調用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 您的示例實際上使用用戶名和密碼而不是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派生,而是以相同的順序復制其方法。 這是.NET COM Interop所必需的。

您不需要手動QueryInterface/Release調用, var cred = (IDsObjectPickerCredentials)iobjectpicker; 足夠。

我還發現應該在Initialize方法之前調用SetCredentials

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM