问题描述
我的 C#、.NET 5.0 Windows 应用程序中的以下代码行在我的应用程序正常运行时抛出异常,但在以管理员身份运行时则不抛出异常。
var pc = UserPrincipal.Current.Context;
异常中的消息是“无法联系服务器”。我无法确定异常的确切类型,因为这是在现场发生的,我正在查看一个日志文件,但遗憾的是未能记录该详细信息。我自己一直无法重现这一点。所以我正在寻找一些“防御性编程”,希望能够绕过它。
这是我可以在不以管理员身份运行的情况下工作的东西吗?是否有一些“更安全”的方法来尝试获取当前上下文?我首先尝试获取它的唯一原因是为当前用户获取一个“友好”名称。喜欢UserPrincipal.displayName
还是UserPrincipal.Surname
?
当我正常运行时。这是异常的栈帧
09:26:21.799 [ 1] ERROR - RegisterTypes: Exception Registering types: The server Could not be contacted.
at System.DirectoryServices.AccountManagement.PrincipalContext.ReadServerConfig(String serverName,ServerProperties& properties)
at System.DirectoryServices.AccountManagement.PrincipalContext.DoServerVerifyAndPropRetrieval()
at System.DirectoryServices.AccountManagement.PrincipalContext..ctor(ContextType contextType,String name,String container,ContextOptions options,String userName,String password)
at System.DirectoryServices.AccountManagement.PrincipalContext..ctor(ContextType contextType)
at System.DirectoryServices.AccountManagement.UserPrincipal.get_Current()
at MyApp.Services.SystemService.Initialize()
at MyApp.Mobile.App.RegisterTypes(IContainerRegistry reg)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)