1

我們有一個ASP.NET站點,部分依賴於登錄憑證的表單身份驗證,但是IPrincipal的實現是完全自定義的。IPrincipal的自定義實現拋出System.SystemException:信任關係

但是,特定服務器上運行的網站時(這有點半硬化,當涉及到安全),應用程序崩潰與下列消息話題調用IPrincipal.IsInRole()時:

System.SystemException :主域和受信域之間的信任關係失敗。

這表示Web服務器和DC之間的通信錯誤,但由於我們的應用程序根本不使用Windows身份驗證,所以我不明白爲什麼它需要與DC通信。

這是我實現:

[Serializable] 
public class CustomPrincipal : IPrincipal 
{ 
    public CustomPrincipal(IUser userObj) 
    { 
     this.Identity = new CustomIdentity(userObj.Id, userObj.Username); 
    } 

    public bool IsInRole(string role) 
    { 
     if (role == null) 
      return false; 

     var roles = HttpContext.Current.Session["authentication-roles"] as string[]; 

     if (roles == null) 
      return false; 

     return Array.IndexOf(roles, role) >= 0; 
    } 

    public IIdentity Identity { get; private set; } 

    public CustomIdentity FullIdentity 
    { 
     get { return (CustomIdentity) this.Identity; } 
    } 
} 

當本地調試它(如果它的工作原理),它是正確的實現,實際上是運行。用法如下:

public override void Render() 
    { 
     var items = this.manager.Items 
      .Where(i => EngineContext.CurrentUser.IsInRole(i.Role.InternalName)); 

在這裏設置一個斷點給了我EngineContext.CurrentUser實際上是CustomPrincipal的實現。

有沒有人遇到過這個? ASP.NET如何仍然可能觸發Interface方法上的任何LDAP查找?

我發現這個,http://support.microsoft.com/kb/976494但在我的環境中,網絡服務器和DC都是2008 R2,所以這不適用。但是,我的事件日誌中存在一些錯誤,表明存在與DC有關的一些通信問題,但由於我們不依賴於LDAP,因此這應該不成問題。

安全系統無法與服務器ldap/ddc.domain.com/xxxxxxxxxxxxx建立安全連接。沒有認證協議可用。

服務器超出我的範圍,這意味着我無法自己解決這個問題,但我確實有一個支持憑單,但出於安全原因可能有意爲此設置(儘管看起來很愚蠢) 。

有沒有人遇到過這個問題?

跟帖:堆棧跟蹤顯示這一點:

at System.Security.Principal.NTAccount.TranslateToSids(IdentityReferenceCollection sourceAccounts, Boolean& someFailed) 
at System.Security.Principal.NTAccount.Translate(IdentityReferenceCollection sourceAccounts, Type targetType, Boolean forceSuccess) 
at System.Security.Principal.WindowsPrincipal.IsInRole(String role) 
at Company.Sites.Manager.ViewComponents.MenuComponent.<Render>b__0(INavigationItem i) 

編輯:

我終於能夠複製我的開發機器上的這個錯誤(我是從DC昨天撤銷我的機器,但沒直到今天再現它)

HttpContext.User實際上是一個WindowsPrincipal默認情況下,它似乎和我的代碼中的錯誤是,我只在登錄時用CustomPrincipal替換它。因此,未經認證的用戶仍然可以獲得WindowsPrincipal,如果您的AD存在信任問題,那麼WindowsPrincipal將會失敗。

我試圖通過調用該上AppStart的

AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.NoPrincipal); 

改變默認的主體但這似乎並沒有踢我如何更改ASP.NET默認的主體?

+1

嘗試在生產計算機上記錄'EngineContext.CurrentUser'的類型。它最有可能不包含您的自定義主體。 – 2010-08-03 09:53:48

回答

1

我認爲這是WindowsAuthenticationModule將WindowsPrincipal添加到HttpContext.User,但刪除仍然給出相同的問題。這是隱含在這篇文章中:

http://msdn.microsoft.com/en-us/library/ff647076.aspx

我試着設置AppDomain.CurrentDomain.SetPrincipalPolicy( PrincipalPolicy.NoPrincipal);

關於appstart和OnAuthenticateRequest的建議,但無濟於事。

然而,這個工作(在OnAuthenticateRequest):

Context.User = new GenericPrincipal(new GenericIdentity(String.Empty), new string[0]); 

我會解決這個現在。感謝大家的輸入!

+0

Global.asax OnAuthenticationRequest – onof 2010-08-03 10:06:56