Get The Username/User Id Of Currently Logged In Us

2019-03-06 11:41发布

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.

var current = users.Where(p => p.AuthenticationStatus == UserAuthenticationStatus.LocallyAuthenticated && p.Type == UserType.LocalUser).FirstOrDefault();
var data = await current.GetPropertyAsync(KnownUserProperties.AccountName);
Username = (string)data;

1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-03-06 11:45
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

查看更多
登录 后发表回答