【问题标题】:Is there a .net core 2.2 way of doing WindowsImpersonationContext?是否有 .net core 2.2 方法来执行 WindowsImpersonationContext?
【发布时间】:2019-05-25 01:19:07
【问题描述】:

这不起作用private static WindowsImpersonationContext impersonationContext;

我需要能够做到这一点

tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);

impersonationContext = tempWindowsIdentity.Impersonate();

然后这个

if (impersonationContext != null)
{
    impersonationContext.Undo();
    impersonationContext = null;
}

我在看这个问题/答案:WindowsImpersonationContext & Impersonate() not found in ASP.Core

但是,这似乎不是一个很好的匹配,是吗?

【问题讨论】:

    标签: asp.net-core .net-core asp.net-core-mvc impersonation asp.net-core-2.1


    【解决方案1】:

    ASP.NET Core 不实现模拟。应用程序使用应用程序标识运行所有请求,使用应用程序池或进程标识。如果您需要代表用户明确执行操作,请使用 WindowsIdentity.RunImpersonated。

    WindowsIdentity.RunImpersonated(user.AccessToken, () =>
        {
            var impersonatedUser = WindowsIdentity.GetCurrent();
            var message =
                $"User: {impersonatedUser.Name}\tState: {impersonatedUser.ImpersonationLevel}";
    
            var bytes = Encoding.UTF8.GetBytes(message);
            context.Response.Body.Write(bytes, 0, bytes.Length);
        });
    

    您可以进一步阅读@Configure Windows Authentication in ASP.NET Core

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-28
      • 2020-01-31
      • 2012-12-09
      • 2020-05-22
      • 1970-01-01
      • 2019-07-01
      • 2019-07-26
      • 1970-01-01
      相关资源
      最近更新 更多