[英]Get Microsoft Account Id from Windows 8 desktop application
尝试查找使用Microsoft帐户登录到Windows 8的用户的活动电子邮件/ ID,假设它不是本地帐户,则将其验证为。
警告:未记录的行为开始。 这个代码可以在Microsoft推送Windows Update的任何时候破解。
创建用户令牌时,会将名为“Microsoft Account \\ YourAccountId”的组添加到用户令牌。 您可以使用它来查找活动用户的Microsoft帐户。
无证行为结束
列出当前用户组名的API是:
使用System.Security.Principal类编写示例要容易得多:
public static string GetAccoutName()
{
var wi= WindowsIdentity.GetCurrent();
var groups=from g in wi.Groups
select new SecurityIdentifier(g.Value)
.Translate(typeof(NTAccount)).Value;
var msAccount = (from g in groups
where g.StartsWith(@"MicrosoftAccount\")
select g).FirstOrDefault();
return msAccount == null ? wi.Name:
msAccount.Substring(@"MicrosoftAccount\".Length);
}
打开注册表编辑器并导航到:
HKEY_LOCAL_MACHINE \\ SOFTWARE \\ Microsoft \\ Windows NT \\ CurrentVersion \\ ProfileList
在ProfileList项下,您将看到SID。 通过单独选择每个,您可以查看值条目,并查看与该特定SID关联的用户名。
参考由Sheng(使用令牌)描述的方法,这里是我们创建的Delphi上的代码,用于获取当前的MS帐户ID:
function GetNameFromSid(ASid: Pointer): string;
var
snu: SID_NAME_USE;
szDomain, szUser : array [0..50] of Char;
chDomain, chUser : Cardinal;
begin
chDomain := 50;
chUser := 50;
if LookupAccountSid(nil, ASID, szUser, chUser, szDomain, chDomain, snu) then
Result := string(szDomain) + '\' + string(szUser);
end;
function GetUserGroups(AStrings: TStrings): Boolean;
var
hAccessToken : tHandle;
ptgGroups : pTokenGroups;
dwInfoBufferSize : DWORD;
psidAdministrators : PSID;
int : integer; // counter
blnResult : boolean; // return flag
ProcessId: Integer;
hWindow, hProcess, TokenHandle: THandle;
si: Tstartupinfo;
p: Tprocessinformation;
const
SECURITY_NT_AUTHORITY: SID_IDENTIFIER_AUTHORITY =
(Value: (0,0,0,0,0,5)); // ntifs
SECURITY_BUILTIN_DOMAIN_RID: DWORD = $00000020;
DOMAIN_ALIAS_RID_ADMINS: DWORD = $00000220;
DOMAIN_ALIAS_RID_USERS : DWORD = $00000221;
DOMAIN_ALIAS_RID_GUESTS: DWORD = $00000222;
DOMAIN_ALIAS_RID_POWER_: DWORD = $00000223;
begin
Result := False;
p.dwProcessId := 0;
hWindow := FindWindow('Progman', 'Program Manager');
GetWindowThreadProcessID(hWindow, @ProcessID);
hProcess := OpenProcess (PROCESS_ALL_ACCESS, FALSE, ProcessID);
if OpenProcessToken(hProcess, TOKEN_QUERY, TokenHandle) then
begin
GetMem(ptgGroups, 1024);
try
blnResult := GetTokenInformation( TokenHandle, TokenGroups,
ptgGroups, 1024,
dwInfoBufferSize );
CloseHandle( TokenHandle );
if blnResult then
begin
for int := 0 to ptgGroups.GroupCount - 1 do
AStrings.Add(GetNameFromSid(ptgGroups.Groups[ int ].Sid));
end;
finally
FreeMem( ptgGroups );
end;
end;
end;
function GetCurrnetMSAccoundId: string;
const
msAccStr = 'MicrosoftAccount\';
var
AGroups: TStrings;
i: Integer;
begin
Result := '';
AGroups := TStringList.Create;
try
GetUserGroups(AGroups);
for i := 0 to AGroups.Count-1 do
if Pos(msAccStr, AGroups[i]) > 0 then
begin
Result := Copy(AGroups[i], Length(msAccStr)+1, Length(AGroups[i])-Length(msAccStr));
Break;
end;
finally
AGroups.Free;
end;
end;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.