2012-02-02 20 views
5

我有一個ASP.NET application它使用針對ADFS的聲明基礎身份驗證。我還通過使用對Windows身份識別服務的聲明將其映射到WindowsClaimsIdentity。這工作正常。冒充ASP.NET聲稱身份到Windows身份

但現在我需要模擬當前的請求/線程,以便我可以訪問一個不聲明的服務。我應該怎麼做?

我應該在Application_PostAuthenticate事件獲得了WindowsImpersonationContext並保存在HttpContext.Items,然後在Application_EndRequest調用Undo方法?

還有其他的首選方法嗎?

更新:由於我沒有得到任何暗示什麼樣的模仿我喜歡嘗試我自己的建議。我創造了這個代碼中的global.asax.cs:

private static readonly string WICKey = typeof(System.Security.Principal.WindowsImpersonationContext).AssemblyQualifiedName; 

    protected void Application_PostAuthenticateRequest() 
    { 
     var wid = User.Identity as System.Security.Principal.WindowsIdentity; 
     if (wid != null) 
     { 
      HttpContext.Current.Trace.Write("PostAuthenticateRequest PreImpersonate: " + System.Security.Principal.WindowsIdentity.GetCurrent().Name); 
      HttpContext.Current.Items[WICKey] = wid.Impersonate(); 
      HttpContext.Current.Trace.Write("PostAuthenticateRequest PostImpersonate: " + System.Security.Principal.WindowsIdentity.GetCurrent().Name); 
     } 
    } 

    protected void Application_EndRequest() 
    { 
     var wic = HttpContext.Current.Items[WICKey] as System.Security.Principal.WindowsImpersonationContext; 
     if (wic != null) 
     { 
      HttpContext.Current.Trace.Write("EndRequest PreUndoImpersonate: " + System.Security.Principal.WindowsIdentity.GetCurrent().Name); 
      wic.Undo(); 
      HttpContext.Current.Trace.Write("EndRequest PostUndoImpersonate: " + System.Security.Principal.WindowsIdentity.GetCurrent().Name); 
     } 
    } 

當我看向跟蹤日誌我看到這個

PostAuthenticateRequest PreImpersonate: NT AUTHORITY\NETWORK SERVICE 
PostAuthenticateRequest PostImpersonate: MyDomain\CorrectUser 
Home: NT AUTHORITY\NETWORK SERVICE 
EndRequest PreUndoImpersonate: NT AUTHORITY\NETWORK SERVICE 
EndRequest PostUndoImpersonate: NT AUTHORITY\NETWORK SERVICE 

所以在第二行,你可以看到線程正確冒充。但在接下來的幾行中,您會看到模擬失敗。 (第三行來自控制器)。

當我使用下面的代碼來冒充本地正常工作:

 var wid = User.Identity as System.Security.Principal.WindowsIdentity; 
     if (wid != null) 
     { 
      using (var ctx = wid.Impersonate()) 
      { 
       //Do something 
      } 
     } 

但我想冒充整個請求壽命。 我該怎麼做?

回答

1

你說後端服務不是聲明意識。你能詳細解釋一下嗎?你的意思是編譯的代碼不是聲明意識,但你有能力修改web.config文件?如果是這樣,那麼你可以嘗試配置後端服務,通過在WSFederationAuthenticationModule,SessionAuthenticationModule和自定義ClaimsAuthorizationManager中進行身份驗證來使用WIF管道進行身份驗證,前提是你還需要authZ。然後,您可以在ASP.NET應用程序調用後端服務時使用WIF的ActAs或OnBehalfOf功能。

+0

後端服務正在使用Windows身份驗證,我無法更改任何配置。除此之外,它只會解決問題,因爲後端服務正在使用Windows身份驗證訪問SQL-Server。那麼我必須在那裏解決我的問題。 – Jaap 2012-02-03 07:31:42

+0

[this](http://www.syfuhs.net/post/2010/09/09/Converting-Claims-to-Windows-Tokens-and-User-Impersonation.aspx)呢?您的ASP.NET應用程序可以爲它調用後端服務的某些代碼塊模擬WindowsPrincipal。 – 2012-02-03 14:19:38

+0

我想模擬PostAuthenticateRequest事件,以便整個請求可以在該模擬的上下文中運行。在PostAuthenticationRequest中模擬呼叫後,WindowsIdentity.GetCurrent()。名稱返回確實是預期的用戶名。但在我的控制器中,然後在EndRequest中它又有了NetworkService帳戶。並沒有做一個模擬的文字。撤消()! – Jaap 2012-02-15 11:16:24

-1

對不起,要挖掘這個舊線程,但爲了您的代碼正常工作,請確保運行您的應用程序的應用程序池的管理管道模式設置爲經典