gpt4 book ai didi

c++ - SEC_WINNT_AUTH_IDENTITY 创建 (SSPI)

转载 作者:行者123 更新时间:2023-11-28 02:31:23 32 4
gpt4 key购买 nike

我正在尝试创建一个 SEC_WINNT_AUTH_IDENTITY 以用于调用 AcquireCredentialsHandle 以获得此身份的凭据。但是我很难创建身份验证日期。

这是用于创建结构的代码:

#define SECURITY_WIN32
#include <Windows.h>
#include <sspi.h>


int main(int argc, char* argv[]) {

SEC_WINNT_AUTH_IDENTITY AuthId;
char* login = "login";
char* domain = "mydomain.com";
char* password = "pass";

AuthId.User = login;
AuthId.UserLength = strlen(login);
AuthId.Domain = domain;
AuthId.DomainLength = strlen(domain);
AuthId.Password = password;
AuthId.PasswordLength = strlen(password);
AuthId.Flags = SEC_WINNT_AUTH_IDENTITY_ANSI;

//Here the call to AcquireCredentialHandle and the end of the program
}

问题是该结构需要 unsigned short __RPC_FAR *User 但我找不到关于此的任何信息以及我应该如何将字符串传递给它。我在网上找到的例子都是直接使用字符串。

最佳答案

在这种情况下,ODBC 的一些经验帮助了我。但是,它不适用于 Unicode 密码,我现在不需要它。但如果有人能给出提示就好了。

好吧,这是我的解决方案。我创建了辅助类:

标题:

class StringParam
{
public:
StringParam();
StringParam(const std::wstring& val);
StringParam(const std::string& val);
virtual ~StringParam();

unsigned short* GetBuffer() const;
unsigned long GetBufferLength() const;
unsigned long GetLength() const;
private:
unsigned short* buffer;
unsigned long bufferLength;
};

实现:

StringParam::StringParam():
buffer(NULL),
bufferLength(0)
{
}

StringParam::StringParam(const std::wstring& val):
StringParam()
{
size_t len = val.size(); // .length() is the same
bufferLength = sizeof(unsigned short) * (len + 1);
unsigned short* buf = new unsigned short[len + 1];
memset(buf, 0, bufferLength);
int i = 0;
for (wchar_t c: val)
buf[i++] = (unsigned short)c;

buffer = reinterpret_cast<unsigned short*>(buf);
}

StringParam::StringParam(const std::string& val):
StringParam()
{
size_t len = val.size(); // .length() is the same
bufferLength = sizeof(unsigned short) * (len + 1);
unsigned short* buf = new unsigned short[len + 1];
memset(buf, 0, bufferLength);
int i = 0;
for (char c: val)
buf[i++] = (unsigned short)c;

buffer = reinterpret_cast<unsigned short*>(buf);
}

StringParam::~StringParam()
{
if (buffer)
{
delete[] buffer;
buffer = NULL;
bufferLength = 0;
}
}

unsigned short* StringParam::GetBuffer() const
{
return buffer;
}

unsigned long StringParam::GetBufferLength() const
{
return bufferLength;
}

unsigned long StringParam::GetLength() const
{
return bufferLength / sizeof(unsigned short);
}

用法:

SECURITY_STATUS res;

// Aquire credentials
CredHandle credentials;
TimeStamp credentialsExpiryTime;

SEC_WINNT_AUTH_IDENTITY userIdentity;
StringParam prmDomain("domain.com");
StringParam prmUser("username");
StringParam prmPassword("password#1");
// wstring version will also work:
//StringParam prmDomain(L"domain.com");
//StringParam prmUser(L"username");
//StringParam prmPassword(L"password#1");

userIdentity.Domain = prmDomain.GetBuffer();
userIdentity.DomainLength = prmDomain.GetLength();
userIdentity.User = prmUser.GetBuffer();
userIdentity.UserLength = prmUser.GetLength();
userIdentity.Password = prmPassword.GetBuffer();
userIdentity.PasswordLength = prmPassword.GetLength();
userIdentity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
res = AcquireCredentialsHandle(NULL, L"Kerberos", SECPKG_CRED_INBOUND, NULL, &userIdentity, NULL, NULL, &credentials, &credentialsExpiryTime);

对我来说效果很好。诀窍在于 wchar_t 的长度为 4 个字节。但是 SSPI 使用 UTF-16。在 ODBC 中,从 wstring 转换的 Unicode 字符有效,但这里有所不同。

关于c++ - SEC_WINNT_AUTH_IDENTITY 创建 (SSPI),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28877812/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com