【问题标题】:UWP : Get the current UserName to provide rights to the userUWP : 获取当前 UserName 为用户提供权限
【发布时间】: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


【解决方案1】:

获取有关域和域用户更改的信息

var data = await current.GetPropertyAsync(KnownUserProperties.AccountName);

var data = await current.GetPropertyAsync(KnownUserProperties.DomainName);

我的结果是: ROM.EUROPE.TECHTEAM.COM\scojocar

【讨论】:

  • 谢谢@Sergiu Cojocaru。但正如我在第一篇文章中提到的,我也尝试使用 UserInfo 示例中给出的解决方案:KnownUserProperties 的所有可用属性都已恢复,但只有 FirstName 和 LastName 有值。我想知道这是否不是因为我使用个人 Outlook 帐户登录到 Windows。 Active Directory 帐户是否会有所不同,因为最终客户必须使用这些帐户?
  • 如果您使用个人帐户,您的名字和姓氏将填充您的数据,当您在域中时,您的名字和姓氏将为空,只有 DomainName 属性将包含有关您的数据域名和用户名
  • 那么结果正常吗?但是如何检索我可以通过命令行“whoami”获得的“主机”详细信息,这给了我:“主机\用户名”?
  • msdn.microsoft.com/de-de/library/windows/apps/…: new Windows.Security.ExchangeActiveSyncProvisioning.EasClientDeviceInformation().friendlyName;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-19
  • 2017-07-04
  • 2013-08-25
  • 2010-09-25
  • 2017-07-04
  • 1970-01-01
  • 2012-12-14
相关资源
最近更新 更多