Recently, a customer complained that our product was not populating the domain name.
Our code uses the Username and the User's Domain Name to query their membership rolls in Active Directory.
In our code, we use Environment.UserDomainName :
var domain = Environment.UserDomainName;
When I looked at this simple line of code, I discovered it could raise the PlatformNotSupportedException and the InvalidOperationException .
Looking for an alternative method of reading the domain name, I came across AppDomain.CurrentDomain :
var domain = AppDomain.CurrentDomain.FriendlyName.Split(".".ToArray())[0];
That is nice, but it also appears to have an AppDomainUnloadedException that could be triggered.
First Question:
Will these 2 methods always return the same information for a given user who is running our program on a local PC?
Second Question:
Is one method less "exception prone" than the other (again, for a given user who is running our program on a local PC)?
Last Question:
Is there a method of getting the local domain name of the user running our application that will not cause an exception to be thrown?
EDIT: For Tyler Lee
Did you even try to see what the value of AppDomain.CurrentDomain.FriendlyName
was?
To Answer question 1: No, they will not.
Question 2 does not seem to apply considering question 1
Question 3: probably not. Environment.UserDomainName
seems to be the source for getting a network domain name, and as you found, it has the potential to generate exceptions. The MSDN page gives details on when those exceptions are thrown, and you should write your code to be able to deal with it if they happen.
The AppDomain.CurrentDomain Property is the domain of the current application and the current thread.
In other words, this is the domain of a project.
In Visual Studio, under a Project's Properties page, this would be referred to as the Project's Assembly Name.
By contrast, Environment.UserDomainName Property is the name of the network that the current user is logged in through.
The two are not related.
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.