Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用C#API通过SSPI对vSphere进行身份验证_C#_Sspi_Vsphere - Fatal编程技术网

使用C#API通过SSPI对vSphere进行身份验证

使用C#API通过SSPI对vSphere进行身份验证,c#,sspi,vsphere,C#,Sspi,Vsphere,我正在使用C#库连接vSphere(VimClient)。我使用此方法登录: VimClient client = new VimClient(); client.Connect("https://vSphereMachine/sdk"); client.Login("userName", "password"); 我登录的用户是进程使用的当前用户。是否有一种方法可以使用当前登录的用户进行身份验证 这条线索似乎提供了一些建议,但我在那里尝试的都不管用: 这可能是因为我不熟悉.NET中的SSPI

我正在使用C#库连接vSphere(VimClient)。我使用此方法登录:

VimClient client = new VimClient();
client.Connect("https://vSphereMachine/sdk");
client.Login("userName", "password");
我登录的用户是进程使用的当前用户。是否有一种方法可以使用当前登录的用户进行身份验证

这条线索似乎提供了一些建议,但我在那里尝试的都不管用:


这可能是因为我不熟悉.NET中的SSPI实现。

由于
VMWare.Vim.dll
只包装了生成的WCF服务客户端,我能够适应直接使用dll。这是我对他的
LoginBySspiPackage
方法的改编

    private UserSession LoginBySspiPackage(SspiPackageType sspiPackage, string serviceSpn)
    {
        Log($"Logging in to VSphere instance {VimClient.ServiceUrl} using SSPI.");
        var sspiClient = new SspiClient(serviceSpn, sspiPackage);
        var sessionManager = new SessionManager(VimClient, VimClient.ServiceContent.SessionManager);

        var serverNotReady = true;
        UserSession session = null;

        while (serverNotReady)
        {
            try
            {
                var base64String = Convert.ToBase64String(sspiClient.Token);
                session = sessionManager.LoginBySSPI(base64String, "en");

                serverNotReady = false; // Connected!  
            }
            catch (VMware.Vim.VimException e)
            {
                if (e.MethodFault is SSPIChallenge)
                {
                    var sspiChallenge = e.MethodFault as SSPIChallenge;
                    var base64String = Convert.FromBase64String(sspiChallenge.Base64Token);
                    sspiClient.Initialize(base64String);
                }
                else if (e.MethodFault is InvalidLogin)
                {
                    throw new InvalidLoginException(e.Message);
                }
                else
                {
                    throw;
                }
            }
        }

        return session;
    }