2010-01-15 41 views
3

我有一個使用Kerberos訪問使用ASP.NET 3.5和IIS的外部資源的Web應用程序。如何在Kerberos中「un-impersonate」(un-delegate?)

當用戶連接到應用程序時,Kerberos身份驗證會自動奇蹟般地允許我連接到充當使用委託的用戶的外部資源。這並不容易。這很好,但我有一個問題。有時我需要使用比用戶擁有更多權限的帳戶連接到外部資源。應用程序池正在運行的服務帳戶具有我需要的附加權限。如何刪除用戶的Kerberos標識並使用運行應用程序池的服務帳戶與Kerberos連接?

UPDATE

我不知道爲什麼我沒有得到響應的都沒有。我以前從來沒有見過。請張貼問題,他們可能會澄清問題(對我來說)。

+0

這是Kerberos的..... – 2010-01-15 18:59:27

回答

5

我有一個類:

public class ProcessIdentityScope : IDisposable 
{ 
    private System.Security.Principal.WindowsImpersonationContext _impersonationContext; 
    private bool _disposed; 

    public ProcessIdentityScope() 
    { 
     _impersonationContext = System.Security.Principal.WindowsIdentity.Impersonate(IntPtr.Zero); 
    } 

    #region IDisposable Members 

    public void Dispose() 
    { 
     Dispose(true); 
     GC.SuppressFinalize(this); 
    } 

    protected virtual void Dispose(bool disposing) 
    { 
     if (!_disposed) 
     { 
      _impersonationContext.Undo(); 
      _impersonationContext.Dispose(); 
      _disposed = true; 
     } 
     else 
      throw new ObjectDisposedException("ProcessIdentityScope"); 
    } 

    #endregion 
} 

我用它像這樣:

using(ProcessIdentityScope identityScope = new ProcessIdentityScope()) 
{ 
    // Any code in here runs under the Process Identity. 
} 

這個代碼是基於這個MSDN文章:http://msdn.microsoft.com/en-us/library/ms998351.aspx

+0

Dang @Ryan,這看起來正是我想要的。我應該有機會明天測試它。 – Hogan 2010-01-19 23:00:00

+0

順便說一句,這個解決方案在生產中很棒,再次感謝@Ryan – Hogan 2010-02-03 22:19:45

+0

@Hogan:很高興能幫到你! – Ryan 2010-02-05 19:16:16