c# – 如何在单元测试中将Thread.CurrentPrincipal重置为unauthe
发布时间:2020-12-15 08:21:19 所属栏目:百科 来源:网络整理
导读:我有可以从多种客户端类型调用的库代码,例如WinForms,Console,ASP.NET等……并且需要确定当前的主体.这样做我正在执行Thread.CurrentPrincipal和Environment.UserName的两步检查,如下所示: var currentUser = !System.Threading.Thread.CurrentPrincipal.Id
我有可以从多种客户端类型调用的库代码,例如WinForms,Console,ASP.NET等……并且需要确定当前的主体.这样做我正在执行Thread.CurrentPrincipal和Environment.UserName的两步检查,如下所示:
var currentUser = !System.Threading.Thread.CurrentPrincipal.Identity.IsAuthenticated ? null : System.Threading.Thread.CurrentPrincipal.Identity.Name; if (string.IsNullOrWhiteSpace(currentUser)) { currentUser = Environment.UserName; } 在控制台应用程序中,Thread.CurrentPrincipal.Identity.IsAuthenticated在MSTest中始终为false howerver,它始终具有有效的经过身份验证的用户. 无论如何,在单元测试中将Thread.CurrentPrincipal的值重置为unauthenticated以模仿Console应用程序? 解决方法
你需要做的就是:
Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(""),new string[0]); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |