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

六月ゝ 毕业季﹏ 提交于 2020-01-11 12:58:32

问题


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:


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



来源:https://stackoverflow.com/questions/36785526/get-the-username-user-id-of-currently-logged-in-user-in-a-uwp-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!