我有可以从多种客户端类型调用的库代码,例如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;
}
Run Code Online (Sandbox Code Playgroud)
在控制台应用程序中,Thread.CurrentPrincipal.Identity.IsAuthenticated在MSTest中始终为false howerver,它始终具有有效的经过身份验证的用户.
无论如何,在单元测试中将Thread.CurrentPrincipal的值重置为unauthenticated以模仿Console应用程序?
Joe*_*Joe 12
你需要做的就是:
Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(""), new string[0]);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3826 次 |
| 最近记录: |