【发布时间】:2016-10-24 18:30:33
【问题描述】:
对于我为客户开发的应用,我需要获取当前用户的详细信息。 然后我必须通过将这对“domain\login”与包含所有授权用户的表进行比较来分配权限。
我重用了@Sergiu Cojocaru here 提供的代码,我将其与官方示例 (UserInfo) 混合以获取更多详细信息:
IReadOnlyList<Windows.System.User> users = await Windows.System.User.FindAllAsync();
var current = users.Where(u => u.AuthenticationStatus == Windows.System.UserAuthenticationStatus.LocallyAuthenticated &&
u.Type == Windows.System.UserType.LocalUser).FirstOrDefault();
// user may have username
var data = await current.GetPropertyAsync(KnownUserProperties.AccountName);
string displayName = (string)data;
// or may be authenticated using hotmail/outlook
if (string.IsNullOrEmpty(displayName))
{
string firstName = (string)await current.GetPropertyAsync(KnownUserProperties.FirstName);
string lastName = (string)await current.GetPropertyAsync(KnownUserProperties.LastName);
displayName = string.Format("{0} {1}", firstName, lastName);
}
但实际上我只从“KnownUserProperties”获得“firstName”和“lastName”。其他属性为空:
- providerName: null
- 账户名:空
- guestHost:空
- principalName:空
- 域名:空
- sipUri: null
=>这正常吗?
问题是预期的一对“domain\login”是基于命令行“whoami”的结果,这并没有给出相同的结果“firstName”和“lastName”。
=> 有没有办法在应用中获得相同的结果?
我指定我使用 hotmail 帐户,但客户帐户必须基于 Active Directory:我不知道这是否会改变什么...
【问题讨论】:
-
如果你想为用户取用户名,检查这个stackoverflow.com/questions/39949829/…
-
谢谢,但这并不是我真正需要的,因为这只给出了用户名,而不是域...
-
对于 domainName 和 sipUri 我可以站在我这边。您是否加入了域?
-
我没有加入域,但问题是我需要使用通过“whoami”命令行恢复的相同格式的数据。
标签: c# uwp windows-10 account