无法在UWP应用中获取某些用户帐户信息-内部活动目录(非Azure AD) [英] Unable to get some user account information in UWP app - In-premise active directory (Not Azure AD)
问题描述
在UWP应用中,我启用了用户帐户信息
功能。
我需要获取$ b当前登录用户的用户名和域名(每个用户分别使用域名)(用户使用内部Active Directory帐户-不是Azure AD 登录)。
例如,用户将使用用户名 user1 $ c登录到Active Directory域
domain1
$ c>。即 domain1\user1
。
我正在使用以下代码尝试获取所需的详细信息:
IReadOnlyList< User>用户=等待User.FindAllAsync();
var user = users.FirstOrDefault();
//获取域
var data1 =等待用户。GetPropertyAsync(KnownUserProperties.DomainName);
string strDomainName =(string)data1;
//获取用户名
var data2 =等待用户。GetPropertyAsync(KnownUserProperties.AccountName);
字符串strUserName =(string)data2;
问题:
-
strDomainName
返回domain1.com\user1
。为什么所有域都包含.com
部分?在c#winforms应用程序上,我们可以轻松获得domain1\user1
。 -
strUserName
返回一个空字符串。即。为什么它不返回任何值?
我还检查了以下内容:
-
KnownUserProperties.FirstName
返回一个空字符串。即 -
KnownUserProperties.LastName
返回一个空字符串。即 -
KnownUserProperties.PrincipalName
返回一个空字符串。即 -
KnownUserProperties.ProviderName
返回一个空字符串。即 -
KnownUserProperties.GuestHost
返回一个空字符串。即
我还需要启用类似于用户帐户信息$ c的其他功能吗? $ c>能力?还是需要授予该应用任何其他权限才能获取此信息?
我知道我可以获得<$ c $的价值c> strDomainName 并执行字符串函数以获取所需的信息。但是我想知道是否有任何直接获取此信息的方法。也很好奇为什么 KnownUserProperties.AccountName
和上面列出的其他属性,例如 FirstName
, LastName
等只是返回一个空字符串。
我正在运行以下版本的Windows:
我将以下设置为目标版本
和最低版本
:
要验证,我还使用
以下设置已在设置
> 隐私
> 帐户中自动启用信息
。
TestApp
是我尝试过的应用,用户信息C#示例
是来自GitHub的示例应用:
更新:
在还启用了之后企业身份验证
功能, KnownUserProperties.PrincipalName
确实返回期望值。即 user1@domain1.com
。
但是,上面列出的其他属性,例如 FirstName
, LastName
等。仅返回一个空字符串,而我仍然找不到返回 domain1\user1
的任何属性(没有 .com
部分)
您尝试访问的信息不可靠,因为不必设置(如您所提到的)并且
我也遇到类似的问题,建议您使用 UWP OneDrive API
使用Microsoft.OneDrive.Sdk;
然后请求 wl.basic
范围。该范围至少包含一个可靠的用户名。
公共静态异步任务< bool> GetAuthenticatedClient()
{
string oneDriveConsumerBaseUrl = https://api.onedrive.com/v1.0;
var作用域= new List< string>
{
wl.signin,
wl.basic,
};
Task authTask;
var onlineIdAuthProvider =新的OnlineIdAuthenticationProvider(scopes.ToArray());
authTask = onlineIdAuthProvider.RestoreMostRecentFromCacheOrAuthenticateUserAsync();
oneDriveClient =新的OneDriveClient(oneDriveConsumerBaseUrl,onlineIdAuthProvider);
AuthProvider = onlineIdAuthProvider;
试试
{
等待authTask;
如果(!AuthProvider.IsAuthenticated)
{
返回false;
}
}
catch(ServiceException异常)
{
//吞下auth异常,但写消息进行调试。
//Debug.WriteLine(exception.Error.Message);
返回false;
}
返回true;
}
对于域,我不确定,但是您可以尝试通过 Environment.UserDomainName
访问它,如 MSDN 或与 Windows.Networking.Connectivity.NetworkInformation.GetHostNames()
如此处。
In a UWP app, I have enabled the User Account Information
capability.
I need to get the username and the domain name (each of them separately) of the currently logged on user (The users are logged on with an in-premise Active Directory account - Not Azure AD).
For example, the user would log in to the Active Directory domain domain1
using the username user1
. i.e. domain1\user1
.
I am using the following code to try to get the required details:
IReadOnlyList<User> users = await User.FindAllAsync();
var user = users.FirstOrDefault();
// get domain
var data1 = await user.GetPropertyAsync(KnownUserProperties.DomainName);
string strDomainName = (string)data1;
// get username
var data2 = await user.GetPropertyAsync(KnownUserProperties.AccountName);
string strUserName = (string)data2;
Issues:
strDomainName
returnsdomain1.com\user1
. Why does this include the.com
part for all our domains? On c# winforms applications we can easily getdomain1\user1
without any issue.strUserName
returns an empty string. i.e. "". Why does this not return any value?
I also checked the following:
KnownUserProperties.FirstName
returns an empty string. i.e. ""KnownUserProperties.LastName
returns an empty string. i.e. ""KnownUserProperties.PrincipalName
returns an empty string. i.e. ""KnownUserProperties.ProviderName
returns an empty string. i.e. ""KnownUserProperties.GuestHost
returns an empty string. i.e. ""
Is there anything else I need to enable similar to the User Account Information
capability? Or are there any other permissions that need to be granted to the app to get this information?
I understand that I can get the value of strDomainName
and perform string functions to get what I need. But I want to know if there is any way to get this information directly. Also curious why KnownUserProperties.AccountName
and other properties listed above such as FirstName
, LastName
etc. just returns an empty string.
I am running the following version of Windows:
I have the following set as the Target version
and Min Version
:
To verify, I also tested with the UserInfo sample project by Microsoft from GitHub and I got the following output:
The following was automatically enabled in Settings
> Privacy
> Account Info
.
TestApp
is the app I tried with and User Info C# Sample
is the sample app from GitHub:
Update:
After also enabling the Enterprise Authentication
capability, KnownUserProperties.PrincipalName
does return the expected value. i.e. user1@domain1.com
.
However, other properties listed above such as FirstName
, LastName
etc. just returns an empty string and I am still unable to find any property that returns domain1\user1
(without the .com
part)
The Information you are trying to access are not reliable, as they (as you mentioned) do not have to be set and also they can be restricted access to via privacy settings in general.
I had a similar problem and would advise you to use the UWP OneDrive API
using Microsoft.OneDrive.Sdk;
and then request wl.basic
scope. This scope contains at least a reliable username.
public static async Task<bool> GetAuthenticatedClient()
{
string oneDriveConsumerBaseUrl = "https://api.onedrive.com/v1.0";
var scopes = new List<string>
{
"wl.signin",
"wl.basic",
};
Task authTask;
var onlineIdAuthProvider = new OnlineIdAuthenticationProvider(scopes.ToArray());
authTask = onlineIdAuthProvider.RestoreMostRecentFromCacheOrAuthenticateUserAsync();
oneDriveClient = new OneDriveClient(oneDriveConsumerBaseUrl, onlineIdAuthProvider);
AuthProvider = onlineIdAuthProvider;
try
{
await authTask;
if (!AuthProvider.IsAuthenticated)
{
return false;
}
}
catch (ServiceException exception)
{
// Swallow the auth exception but write message for debugging.
//Debug.WriteLine(exception.Error.Message);
return false;
}
return true;
}
As for the domain, I'm not sure, but you could try to access it via Environment.UserDomainName
like described on MSDN or with Windows.Networking.Connectivity.NetworkInformation.GetHostNames()
like described here.
这篇关于无法在UWP应用中获取某些用户帐户信息-内部活动目录(非Azure AD)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!