0

I already tried the folowing:

 IReadOnlyList<User> users = await User.FindAllAsync();
 var current = users
      .Where( p =>
          p.AuthenticationStatus == UserAuthenticationStatus.LocallyAuthenticated &&
          p.Type == UserType.LocalUser
      )
      .FirstOrDefault();

 string dominio = (string) await current.GetPropertyAsync( KnownUserProperties.DomainName ); // cause an error

Not worked: "A method was called at an unexpected time"

How do I get the domain name or domainname\username?

Dai
  • 141,631
  • 28
  • 261
  • 374
Luiz
  • 198
  • 2
  • 13
  • 1
    Which method call caused the exception? Where in your program are you making this call? – Dai Dec 26 '17 at 15:40
  • Sorry. Edited the question – Luiz Dec 26 '17 at 15:41
  • If the program is running on the user's machine, then you can use `string dominio = Environment.UserDomainName;` – Dave Smash Dec 26 '17 at 15:52
  • Enviroment.UserDomainName not work because it is a Windows 10 machine and I'm trying to do a UWP app – Luiz Dec 26 '17 at 15:54
  • Are you using windows 10 professional – Maytham Fahmi Dec 26 '17 at 16:05
  • I'm using a VM running Windows 10 Enterprise Insider Preview – Luiz Dec 26 '17 at 16:15
  • If a user is "Locally Authenticated", do they belong to a domain? And this questions sounds a lot like [Unable to get some user account information in UWP app...](https://stackoverflow.com/questions/42952503/unable-to-get-some-user-account-information-in-uwp-app-in-premise-active-direc) question. – Black Frog Dec 26 '17 at 16:41
  • Yes, I saw that thread but did not helped. I'm still getting the "A method was called at an unexpected time" – Luiz Dec 27 '17 at 10:58

1 Answers1

0

Finnaly worked.

using Windows.Networking;
using Windows.Networking.Connectivity;


var hostNames = NetworkInformation.GetHostNames();
var hostName = hostNames.FirstOrDefault(name => 
         name.Type == HostNameType.DomainName)?
         .DisplayName ?? "???";

From: How to get local host name in C# on a Windows 10 universal app

Luiz
  • 198
  • 2
  • 13