试图找出使用 Microsoft 帐户登录到 Windows 8 的用户的活动电子邮件/ID,假设它不是他们经过身份验证的本地帐户。
- 试图从 WPF 桌面 C# 应用程序而不是 Windows 应用商店应用程序中找出它
- 发现 Live SDK 可能相关,例如 me 快捷方式,但不确定此 API 是否可用于成熟的 .NET 应用程序?
试图找出使用 Microsoft 帐户登录到 Windows 8 的用户的活动电子邮件/ID,假设它不是他们经过身份验证的本地帐户。
警告:无证行为开始。每当 Microsoft 推送 Windows 更新时,此代码可能会中断。
当您的用户令牌创建时,名为“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;