简体   繁体   English

UWP:获取当前的用户名以向用户提供权限

[英]UWP : Get the current UserName to provide rights to the user

For an app that I develop for a customer, I need to get the current user details. 对于我为客户开发的应用程序,我需要获取当前的用户详细信息。 Then I must assign rights by comparing the couple " domain\\login " to a table containing all the authorized users. 然后,我必须通过将夫妇“ domain \\ login ”与包含所有授权用户的表进行比较来分配权限。

I reuse the code given by @Sergiu Cojocaru here , that I have mixed with an official sample ( UserInfo ) to get more details: 我在这里重用@Sergiu Cojocaru给出的代码,我将其与官方示例( 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);
}

But actually I only get the " firstName " and the " lastName " from " KnownUserProperties ". 但是实际上我只能从“ KnownUserProperties ”获得“ firstName ”和“ lastName ”。 The others properties are empties: 其他属性为空:

  • providerName: null providerName:空
  • accountName: null accountName:空
  • guestHost: null guestHost:空
  • principalName: null PrincipalName:null
  • domainName: null domainName:null
  • sipUri: null sipUri:null

=> Is this normal? =>这正常吗?

The problem is that the expected couple " domain\\login " is based on the result of the command line " whoami ", that doesn't give the same result that " firstName " and " lastName ". 问题在于,预期的对“ domain \\ login ”是基于命令行“ whoami ”的结果,与“ firstName ”和“ lastName ”的结果不同。

=> Is there a way to get the same result in the app? =>有没有办法在应用程序中获得相同的结果?

I specify that I use a hotmail account but the customers accounts must be based on Active Directory: I don't know if this will change anything... 我指定使用Hotmail帐户,但客户帐户必须基于Active Directory:我不知道这是否会更改任何内容...

To get information about domain and domain user change 获取有关域和域用户更改的信息

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

to

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

on my side result is: ROM.EUROPE.TECHTEAM.COM\\scojocar 在我这边的结果是:ROM.EUROPE.TECHTEAM.COM \\ scojocar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM