Get The Username/User Id Of Currently Logged In User In A UWP App

前端 未结 1 1232
轮回少年
轮回少年 2021-01-26 03:35

I want to get the username or user id of the currently logged in user in a UWP app. Below is the code that I am using but it returns null.

相关标签:
1条回答
  • 2021-01-26 04:29
    var users = await User.FindAllAsync(UserType.LocalUser);
    var user = (string) await users.FirstOrDefault().GetPropertyAsync(KnownUserProperties.AccountName);
    var domain = "";
    var host = "";
    
    if (string.IsNullOrEmpty(user))
    {
    var domainWithUser = (string) await users.FirstOrDefault().GetPropertyAsync(KnownUserProperties.DomainName);
    domain = domainWithUser.Split('\\')[0];
    user = domainWithUser.Split('\\')[1];
    }
    

    This helped me. reference http://codegur.com/33736983/get-environment-variables-in-net-core-uwp

    0 讨论(0)
提交回复
热议问题