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 "domainlogin" to a table containing all the authorized users.
我重用了@Sergiu Cojocaru 给出的代码 此处 ,我已将其与官方示例(UserInfo) 以获取更多详细信息:
I reuse the code given by @Sergiu Cojocaru here , that I have mixed with an official sample (UserInfo) to get more details:
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".其他属性为空:
But actually I only get the "firstName" and the "lastName" from "KnownUserProperties". The others properties are empties:
- 提供商名称:空
- 账户名:空
- guestHost:空
- 委托人姓名:空
- 域名:空
- sipUri:空
=>这正常吗?
问题是预期的一对domainlogin"是基于命令行whoami"的结果,这不会给出相同的结果那个firstName"和lastName".
The problem is that the expected couple "domainlogin" is based on the result of the command line "whoami", that doesn't give the same result that "firstName" and "lastName".
=> 有没有办法在应用中获得相同的结果?
我指定我使用的是 hotmail 帐户,但客户帐户必须基于 Active Directory:我不知道这是否会改变任何事情...
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...
推荐答案
获取有关域和域用户更改的信息
To get information about domain and domain user change
var data = await current.GetPropertyAsync(KnownUserProperties.AccountName);
到
var data = await current.GetPropertyAsync(KnownUserProperties.DomainName);
我的结果是:ROM.EUROPE.TECHTEAM.COMscojocar
on my side result is: ROM.EUROPE.TECHTEAM.COMscojocar
这篇关于UWP : 获取当前用户名以向用户提供权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!