Warning: undocumented behavior begins. this code can break any time Microsoft pushes a Windows Update.
when your user token is created a group named "Microsoft Account\YourAccountId" is added to the user token. You can use it to find the active user's Microsoft Account.
undocumented behavior ends
The API to list the current user's group names are :
- OpenProcessToken GetCurrentProcess TOKEN_QUERY to get the process token
- GetTokenInformation TokenGroups to get the groups in the token
- LookupAccountSid to get group names
It is much easier to write an example using System.Security.Principal classes:
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);
}