首页 文章

ASP.NET CORE 1.0,模拟

提问于
浏览
7

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

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

数据库连接字串:

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

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

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

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

2 回答

  • 8

    Core不支持模拟,因为所有Web代码都不在proc中,由Kestrel托管 . 如果你想这样做,你需要将当前的Principal作为WindowsPrincipal,然后在需要的时候使用manually impersonate .

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

  • 8

    如果您希望每个页面请求都模拟用户,您可以配置自己的Middleware以在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<Impersonate>();
        app.UseMvc(...);
        ...
    }
    

相关问题