简体   繁体   中英

Universal Windows Application Runtime component not resulting UserIdentity on Windows 10 using Visual Studio 2015

I am using Windows 10 machine and Visual studio 2015 for a project which has Windows Run time component and Universal Windows application (UWA). UWA has reference of Windows Runtime component which perform a task to get UserIdentity (Window NT ID ie "DomainName/UserName") of logged in user. UWA test project is created to debug the Runtime component. If we get UserIdentity in runtime component so we can use component reference to our Windows10 cordova project to get UserIdentity in javascript.
Here we are facing issue where none of the code is working to get logged in user's DomainName/username. We have upgraded this code from Windows 8.1 to 10 wears this code is working great with Windows 8.1 but having issue in Windows 10. Below is the code which we are using to get UserIdentity:

public sealed class getNTID
{
    public static IAsyncOperation<string> DownloadAsStringsAsync()
    {

        return Task.Run<string>(async () =>
        {

            IReadOnlyList<User> users = await User.FindAllAsync();

            var current = users.Where(p => p.AuthenticationStatus == UserAuthenticationStatus.LocallyAuthenticated &&
                                    p.Type == UserType.LocalUser).FirstOrDefault();


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

            return data.ToString();

        }).AsAsyncOperation();
    }
} 

We are getting empty string in data variable at the end. It would be great if someone share their exeperience or they have faced this kind of issue with mentioned platform.

User class is new for windows 10 which is different with UserInformation in windows 8.1 you used. Getting access to a user's personal information is a privilege you have to request, so in your store app's Package.appxmanifest, you'll need to enable the User Account Information capability in the Capabilities tab.

When you call GetPropertyAsync , your user will get a permission prompt from the system as follows,accept that then you can get the permission. More details about capability please reference this document . 在此处输入图片说明

For GetPropertyAsync method, if the property is missing or unavailable, an empty string is returned. So if you have already done but you still get null value maybe that the property is missing or unavailable.

Here is the result I got by running your code to get DomainName of current user (I also put the code in the windows run-time component).

在此处输入图片说明

More details please reference the official sample .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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