Pergunta

We use this code to get the email of the currently logged in user:

var currentUsersEmail = UserPrincipal.Current.EmailAddress;

This works fine when on the network. However, we have laptop users who run our code remotely and in a disconnected state. The above code causes this exception when in this state:

System.DirectoryServices.AccountManagement.PrincipalServerDownException
Message: "The server could not be contacted."

Does anyone know a way to retrive the current user's email that will work both when connected / on the network and when disconnected?

UPDATE: In case you are wondering why we want to do this. Our system has a bunch of methods that send alerts via email. When our developers are running automated tests, we want the emails to be sent to the current logged in user (i.e. the developer) so they can verify the format.

Foi útil?

Solução

You could rewrite your application so that it uses UserPrincipal.Current.EmailAddress the first time, and perhaps whenever it is online, and then saves that information in the user's AppData folder or registry for reference when working offline. It would require running the application at least one online before working offline, but I don't see how else you could do it without having to ask the user or use a config file.

There is simply no way the System.DirectoryServices.AccountManagement namespace will work offline. It's like asking how can a database driven application work without access to the database.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top