Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Microsoft Account Id from Windows 8 desktop application

enter image description here

Trying to find out the active email / id of the user logged into Windows 8 with a Microsoft Account, assuming it is not a local account they are authenticated as.

  • Trying to find that out from a WPF desktop C# application, not a Windows Store app
  • Found the Live SDK to be potentially relevant, e.g. the me shortcut, but am not sure this API can be used from a full-fledged .NET application?
like image 325
Cel Avatar asked Feb 14 '23 19:02

Cel


1 Answers

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);
}
like image 76
Sheng Jiang 蒋晟 Avatar answered Feb 20 '23 02:02

Sheng Jiang 蒋晟