RasGetCredentialsA 函数 (ras.h)

RasGetCredentials 函数检索与指定 RAS 电话簿条目关联的用户凭据。

语法

DWORD RasGetCredentialsA(
  [in]      LPCSTR            unnamedParam1,
  [in]      LPCSTR            unnamedParam2,
  [in, out] LPRASCREDENTIALSA unnamedParam3
);

参数

[in] unnamedParam1

指向 null终止字符串的指针,该字符串指定电话簿 (PBK) 文件的完整路径和文件名。 如果此参数 NULL,则该函数使用当前的默认电话簿文件。 默认电话簿文件是用户在 用户首选项拨号网络 对话框中选择的文件。

[in] unnamedParam2

指向指定电话簿条目名称的 null终止字符串的指针。

[in, out] unnamedParam3

指向 RASCREDENTIALS 结构的指针,该结构在输出中接收与指定电话簿条目关联的用户凭据。

在输入时,将结构的 dwSize 成员设置为 sizeof(RASCREDENTIALS),并将 dwMask 成员设置为指示要检索的凭据信息。 函数返回时,dwMask 指示已成功检索的成员。

返回值

如果函数成功,则返回值 ERROR_SUCCESS

如果函数失败,则返回值是以下错误代码之一或来自 路由和远程访问错误代码 或 Winerror.h 的值。

价值 意义
ERROR_CANNOT_OPEN_PHONEBOOK
找不到指定的电话簿。
ERROR_CANNOT_FIND_PHONEBOOK_ENTRY
电话簿中不存在指定的条目。
ERROR_INVALID_PARAMETER
lpCredentials 参数 NULL
ERROR_INVALID_SIZE
dwSizeRASCREDENTIALS 结构的成员是无法识别的值。

言论

RasGetCredentials 函数检索最后一个用户的凭据,以便使用指定的电话簿条目进行连接,或者随后在调用 RasSetCredentials 函数进行电话簿输入时指定的凭据。

此函数是安全检索与 RAS 电话簿条目关联的凭据的首选方法。 RasGetCredentials 取代 RasGetEntryDialParams 函数,该函数在将来的 Windows 版本中可能不受支持。

RasGetCredentials 不返回实际密码。 相反,szPasswordRASCREDENTIALS 结构的成员包含已保存密码的句柄。 将此句柄替换为对 RasSetCredentialsRasDial的后续调用中保存的密码。 当显示此句柄时,RasDial 检索和使用保存的密码。 此句柄的值可能会在操作系统的未来版本中更改;不开发依赖于此值的内容或格式的代码。

RASCREDENTIALSdwMask 成员包含RASCM_Password标志(如果系统为指定条目保存了密码)。 如果系统没有为此条目保存密码,dwMask 不包含RASCM_Password。

Windows 2000/NT:不支持此功能

如果 RASCREDENTIALS 结构的 dwMask 包含RASCM_DefaultCreds标志,则返回的凭据是所有用户连接的默认凭据。

若要检索预共享密钥,请使用 RASCREDENTIALS.dwMask 字段中的RASCM_PreSharedKey标志。

Windows 2000/NT:不支持此功能

以下示例代码创建“RasEntryName”电话簿条目,使用 RasSetCredentials设置其凭据,然后使用 RasGetCredentials检索这些凭据。

#include <windows.h>
#include "ras.h"
#include <stdio.h>
#include <tchar.h>
#include "strsafe.h"

#define PHONE_NUMBER_LENGTH 7
#define DEVICE_NAME_LENGTH 5
#define DEVICE_TYPE_LENGTH 5
#define DOMAIN_NAME_LENGTH 9
#define USER_NAME_LENGTH 11

DWORD __cdecl wmain(){

    DWORD dwRet = ERROR_SUCCESS;    
    LPTSTR lpszEntry = L"RasEntryName";
    LPTSTR lpszPhoneNumber = L"5555555";
    LPTSTR lpszDeviceName = L"Modem";
    LPTSTR lpszDeviceType = RASDT_Modem;
    LPTSTR lpszDomainName = L"RASDomain";
    LPTSTR lpszUserName = L"RASUserName";
    /***********************************************************************************************/
    // Create a new phone book entry
    /***********************************************************************************************/
  
    // Allocate heap memory for the RASENTRY structure
    LPRASENTRY lpentry = (LPRASENTRY)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RASENTRY));
    if (lpentry == NULL){
        wprintf(L"HeapAlloc failed!\n");
        return 0;
    }
    // The RASENTRY->dwSize member has to be initialized or the RRAS RasValidateEntryName() and 
    // RasSetEntryProperties APIs will fail below.
    lpentry->dwSize = sizeof(RASENTRY);
    lpentry->dwFramingProtocol = RASFP_Ppp;
    lpentry->dwfOptions = 0;
    lpentry->dwType = RASFP_Ppp;
    dwRet |= StringCchCopyN(lpentry->szLocalPhoneNumber, RAS_MaxPhoneNumber, lpszPhoneNumber, PHONE_NUMBER_LENGTH);
    dwRet |= StringCchCopyN(lpentry->szDeviceName, RAS_MaxDeviceName, lpszDeviceName, DEVICE_NAME_LENGTH);
    dwRet |= StringCchCopyN(lpentry->szDeviceType, RAS_MaxDeviceType, lpszDeviceType, DEVICE_TYPE_LENGTH);
    if (dwRet != ERROR_SUCCESS){
        wprintf(L"RASENTRY structure initialization failed!\n");
        HeapFree(GetProcessHeap(), 0, lpentry);
        return 0;
    }

    // Validate the new entry's name
    dwRet = RasValidateEntryName(NULL, lpszEntry);
    if (dwRet != ERROR_SUCCESS){
        wprintf(L"RasValidateEntryName failed: Error = %d\n", dwRet);
        HeapFree(GetProcessHeap(), 0, lpentry);
        return 0;
        }

    // Create and set the new entry's properties
    dwRet = RasSetEntryProperties(NULL, lpszEntry, lpentry, lpentry->dwSize, NULL, 0);
    if (dwRet != ERROR_SUCCESS){
        wprintf(L"RasSetEntryProperties failed: Error = %d\n", dwRet);
        HeapFree(GetProcessHeap(), 0, lpentry);
        return 0;
    }

    /******************************************************************************************/
    // Set and get the new entry's credentials
    /******************************************************************************************/

    // Allocate heap memory for the RASCREDENTIALS structure
    LPRASCREDENTIALS lpCred = (LPRASCREDENTIALS) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RASCREDENTIALS));
    if (lpCred == NULL){
        wprintf(L"HeapAlloc failed!\n");
        return 0;
    }
    // The RASCREDENTIALS->dwsize member must be initialized or the RRAS RasSetCredentials() and 
    // RasGetCredentials() APIs will fail below
    lpCred->dwSize = sizeof(RASCREDENTIALS);

    // The entry's credentials must first be set with RasSetCredentials() before they can be 
    // retrieved with RasGetCredentials(). The values below are used to set the new entry's credentials.
    dwRet |= StringCchCopyN(lpCred->szDomain, DNLEN, lpszDomainName, DOMAIN_NAME_LENGTH);
    dwRet |= StringCchCopyN(lpCred->szUserName, UNLEN, lpszUserName, USER_NAME_LENGTH);
    if (dwRet != ERROR_SUCCESS){
        wprintf(L"RASCREDENTIALS structure initialization failed!\n");
        HeapFree(GetProcessHeap(), 0, lpCred);
        return 0;
    }
    // The username, password, and Domain credentials are valid
    lpCred->dwMask = RASCM_UserName | RASCM_Password | RASCM_Domain;
    
    // Set the newly created entry's credentials
    dwRet = RasSetCredentials(NULL, lpszEntry, lpCred, FALSE);
    
    // The same RASCREDENTIALS structure is used to 'set' and 'get' the credentials. Therefore, zero out 
    // its values. (this proves RasGetCredentials works below!) 
    dwRet |= StringCchCopyN(lpCred->szDomain, DNLEN, L"", 0);
    dwRet |= StringCchCopyN(lpCred->szUserName, UNLEN, L"", 0);
    dwRet |= StringCchCopyN(lpCred->szPassword, UNLEN, L"", 0);
    if (dwRet != ERROR_SUCCESS){
        wprintf(L"RASCREDENTIALS structure reset failed!\n");
        HeapFree(GetProcessHeap(), 0, lpCred);
        HeapFree(GetProcessHeap(), 0, lpentry);
        return 0;
    }

    // Grab the newly created entry's credentials
    dwRet = RasGetCredentials(NULL, lpszEntry, lpCred);
    if(dwRet == ERROR_SUCCESS){
        wprintf(L"The following credentials were retrieved for the entry: %s\n\tUser name: %s\n\tPassword: %s\n\tDomain: %s\n", lpszEntry, lpCred->szUserName, lpCred->szPassword, lpCred->szDomain);
    }else{
        wprintf(L"RasValidateEntryName failed: Error = %d\n", dwRet);
    }

    // Clean up: delete the new entry
    dwRet = RasDeleteEntry(NULL, lpszEntry);
    if (dwRet != ERROR_SUCCESS){
        wprintf(L"RasDeleteEntry failed: Error = %d\n", dwRet);
    }

    HeapFree(GetProcessHeap(), 0, lpentry);
    HeapFree(GetProcessHeap(), 0, lpCred);
    return 0;
}

注意

ras.h 标头将 RasGetCredentials 定义为一个别名,该别名根据 UNICODE 预处理器常量的定义自动选择此函数的 ANSI 或 Unicode 版本。 将中性编码别名与不中性编码的代码混合使用可能会导致编译或运行时错误不匹配。 有关详细信息,请参阅函数原型的 约定。

要求

要求 价值
最低支持的客户端 Windows 2000 Professional [仅限桌面应用]
支持的最低服务器 Windows 2000 Server [仅限桌面应用]
目标平台 窗户
标头 ras.h
Rasapi32.lib
DLL Rasapi32.dll

另请参阅

RASCREDENTIALS

RasGetEntryDialParams

RasSetCredentials

远程访问服务(RAS)概述

远程访问服务函数