我编写了一个自定义Windows Credentials Provider来捕获用户登录。该提供程序的主要目的之一是存储用户密码,并在登录时在Windows上运行的C#服务中使用它。

这导致了我在哪里以及如何存储凭据信息的问题。在登录时(即用户提供有效的登录信息并选择登录),该服务应能够获取并使用该信息。

将凭证信息传达给服务的正确方法是什么?

根据提供的Microsoft凭据提供程序示例,使用凭据登录用户的部分或多或少是其中GetSerialization如下所示:

hr = ProtectIfNecessaryAndCopyPassword(_rgFieldStrings[SFI_PASSWORD], _cpus, &pwzProtectedPassword);

g_pwzProtectedPassword = pwzProtectedPassword;

if (SUCCEEDED(hr))
{
    PWSTR pszDomain;
    PWSTR pszUsername;
    hr = SplitDomainAndUsername(_pszQualifiedUserName, &pszDomain, &pszUsername);
    if (SUCCEEDED(hr))
    {
        KERB_INTERACTIVE_UNLOCK_LOGON kiul;
        hr = KerbInteractiveUnlockLogonInit(pszDomain, pszUsername, pwzProtectedPassword, _cpus, &kiul);
        if (SUCCEEDED(hr))
        {
            // We use KERB_INTERACTIVE_UNLOCK_LOGON in both unlock and logon scenarios.  It contains a
            // KERB_INTERACTIVE_LOGON to hold the creds plus a LUID that is filled in for us by Winlogon
            // as necessary.
            hr = KerbInteractiveUnlockLogonPack(kiul, &pcpcs->rgbSerialization, &pcpcs->cbSerialization);
            if (SUCCEEDED(hr))
            {
                ULONG ulAuthPackage;
                hr = RetrieveNegotiateAuthPackage(&ulAuthPackage);
                if (SUCCEEDED(hr))
                {
                    pcpcs->ulAuthenticationPackage = ulAuthPackage;
                    pcpcs->clsidCredentialProvider = CLSID_CredentialProvider;
                    // At this point the credential has created the serialized credential used for logon
                    // By setting this to CPGSR_RETURN_CREDENTIAL_FINISHED we are letting logonUI know
                    // that we have all the information we need and it should attempt to submit the
                    // serialized credential.
                    *pcpgsr = CPGSR_RETURN_CREDENTIAL_FINISHED;
                }
            }
        }
        CoTaskMemFree(pszDomain);
        CoTaskMemFree(pszUsername);
    }
    CoTaskMemFree(pwzProtectedPassword);
}


编辑:

我试图使用Windows Credential Manager API来保存密码,但这似乎无效。当我尝试获取登录后的用户名/密码时,我什么也没得到。

DWORD cbCreds = (DWORD)(1 + strlen(password));

CREDENTIALW cred = { 0 };
cred.Type = CRED_TYPE_GENERIC;
cred.TargetName = TargetName;
cred.CredentialBlobSize = cbCreds;
cred.CredentialBlob = (LPBYTE)password;
cred.Persist = CRED_PERSIST_LOCAL_MACHINE;
cred.UserName = *username;

return ::CredWriteW(&cred, 0) ? true : false;

最佳答案

认为管道将是与服务进行通信的正确方式。
使用Windows Credential Manager保存/加载密码无效,因为登录会话不同且未登录时用户的SID已禁用。

关于c++ - 从凭证提供者向C#服务提供凭证信息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28826510/

10-15 00:38