我需要在我的Xamarin UWP应用中使用Windows身份验证。如何在应用程序中访问当前登录的用户详细信息。需要检索当前登录Windows的用户Active Directory登录ID。
我已经尝试了下面的解决方案,它给我的结果是空的。
How can I get username or id of currently logged in user in UWP App
感谢你的帮助...
发布于 2018-05-10 14:42:50
如果你没有在Package.appxmanifest中向你的应用程序添加功能,你将没有访问用户帐户信息的权限。

出于其他原因,如果你使用hotmail进行身份验证,你需要KnownUserProperties.FirstName和KnownUserProperties.LastName来获取你的帐户名。
private async void GetUser()
{
IReadOnlyList<User> users = await User.FindAllAsync();
var current = users.Where(p => p.AuthenticationStatus == UserAuthenticationStatus.LocallyAuthenticated &&
p.Type == UserType.LocalUser).FirstOrDefault();
// user may have username
var data = await current.GetPropertyAsync(KnownUserProperties.AccountName);
string displayName = (string)data;
// authinticated using hotmail
if (String.IsNullOrEmpty(displayName))
{
string a = (string)await current.GetPropertyAsync(KnownUserProperties.FirstName);
string b = (string)await current.GetPropertyAsync(KnownUserProperties.LastName);
displayName = string.Format("{0} {1}", a, b);
}
}请注意,以上代码仅适用于UWP原生项目,不能直接在pcl中使用,需要通过DependencyService创建GetUser方法。
更新
如果你已经使用ADAL授权,你可以使用AcquireTokenSilentAsync方法从令牌缓存中静默获取信息,更多信息请参考this。
发布于 2018-05-11 13:00:41
这是一个使用ADAL的UWP sample。ADAL.NET不直接从身份验证上下文公开用户的概念。它确实提供了一个UserInfo,作为AuthenticationResult的一个属性。当您获得身份验证结果时,您可以使用UserInfo属性来获取登录用户的可显示ID。
ADAL wiki新闻将带来详细报道。
发布于 2018-07-11 04:42:16
这是相当简单的。您应该使用Windows.System.User在特定平台级别执行此操作,以检索当前用户的信息。Here is a post,详细描述了如何实现这一点。
https://stackoverflow.com/questions/50246706
复制相似问题