C# 以排队用户的身份在其他进程中运行后台作业

C# 以排队用户的身份在其他进程中运行后台作业,c#,.net,asp.net-web-api,windows-authentication,hangfire,C#,.net,Asp.net Web Api,Windows Authentication,Hangfire,在我们的应用程序中,我们有Web API队列作业要在后台运行。我们使用HangFire进行作业的后台处理。Web API使用Windows身份验证。Hangfire服务器配置为作为windows服务运行 我正试图以排队的用户的身份执行后台作业 我尝试传递WindowsIdentity.GetCurrent()(由hangfire序列化并传递)引发的异常是“模拟的无效令牌-无法复制” 遇到一个打电话的人。但由于这需要密码作为输入,所以不确定如何使用它 有没有办法以排队的同一用户的身份执行后台作业?

在我们的应用程序中,我们有Web API队列作业要在后台运行。我们使用HangFire进行作业的后台处理。Web API使用Windows身份验证。Hangfire服务器配置为作为windows服务运行

我正试图以排队的用户的身份执行后台作业

我尝试传递WindowsIdentity.GetCurrent()(由hangfire序列化并传递)引发的异常是“模拟的无效令牌-无法复制”

遇到一个打电话的人。但由于这需要密码作为输入,所以不确定如何使用它

有没有办法以排队的同一用户的身份执行后台作业?

可能的解决方案:

  • 使用Win32 API调用。缺点是,此方法需要用户密码。更多详情请参见下文
  • 使用Kerberos扩展“用户登录服务”
  • 可能的解决办法:

  • 使用Win32 API调用。缺点是,此方法需要用户密码。更多详情请参见下文
  • 使用Kerberos扩展“用户登录服务”

  • WindowsIdentity
    对象引用句柄,该句柄是内核对象,因此无法序列化。Hangfire必须在作业排队时显式复制用户的令牌。(我不知道它是否已经能做到这一点,或者如果不能,添加起来有多难。)好的,谢谢@HarryJohnston!
    WindowsIdentity
    对象引用句柄,该句柄是内核对象,因此无法序列化。Hangfire必须在作业排队时显式复制用户的令牌。(我不知道它是否已经能做到这一点,或者如果不能,添加起来有多难。)好的,谢谢@HarryJohnston!
    [HttpGet, Route("enq")]
    public IHttpActionResult EnQueue(string country)
    {     
        var curUser = System.Security.Principal.WindowsIdentity.GetCurrent();           
        var id = Hangfire.BackgroundJob.Enqueue(() => Services.Common.TestClass.Test(curUser , country));
        return Ok(id);
    }
    
    var upn = System.DirectoryServices.AccountManagement.UserPrincipal.Current.UserPrincipalName;
    WindowsIdentity s4u = new WindowsIdentity(upn);