ASP.NET CORE 1.0,模拟

我正在写一个Intranet应用程序。 project.json中的目标框架是dnx451。 这是我的发布命令:

dnu publish --runtime dnx-clr-win-x86.1.0.0-rc1-update1 --no-source 

Database Connection字符串:

 Server=name;Database=name;Trusted_Connection=True; 

我正在尝试模拟数据库访问,但它无法正常工作。 当我启动应用程序时,我的Windows用户被识别,它在右上角显示Hello,Domain \ Username。 一旦我尝试访问数据库,我就会收到错误“登录用户域\ Computername失败”。 如果我在我的用户下运行应用程序池,那么一切正常。

IIS:.NET CLR Versio是v4.0,托管Pipline模式Classic和Identity是ApplicationPoolIdentity。 网站身份validation:启用了ASP.NET模拟和Windows身份validation。

我需要做些什么才能改变模仿终于有效?

Core不支持模拟,因为所有Web代码都是由Kestrel托管的proc。 如果你想这样做,你需要将当前的Principal作为WindowsPrincipal,然后在你需要的地方手动模拟 。

需要注意的一点是,在RC1中,您没有获得WindowsPrincipal,因此您现在无法执行此操作。 它将在RC2中修复。

如果您希望每个页面请求都模拟用户,您可以配置自己的中间件在MVC之前运行;

 public class Impersonate { private readonly RequestDelegate next; public Impersonate(RequestDelegate next) { this.next = next; } public async Task Invoke(HttpContext context) { var winIdent = context.User.Identity as WindowsIdentity; if (winIdent == null) { await next.Invoke(context); }else { WindowsIdentity.RunImpersonated(winIdent.AccessToken, () => { next.Invoke(context).Wait(); }); } } } public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { .... app.UseMiddleware(); app.UseMvc(...); ... }