简体   繁体   English

离线时让Windows登录用户的电子邮件

[英]Get Windows logged in user's email when offline

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 (ie the developer) so they can verify the format. 当我们的开发人员运行自动化测试时,我们希望将电子邮件发送给当前登录的用户(即开发人员),以便他们可以验证格式。

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. 您可以重写您的应用程序,以UserPrincipal.Current.EmailAddress首次使用UserPrincipal.Current.EmailAddress ,并且可能在每次联机时使用,然后将该信息保存在用户的AppData文件夹或注册表中,以供脱机工作时参考。 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. System.DirectoryServices.AccountManagement命名空间根本无法脱机工作。 It's like asking how can a database driven application work without access to the database. 这就像问一个数据库驱动的应用程序如何在不访问数据库的情况下工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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