2009-12-10 69 views
19

我有一個基本的MVC 2測試版應用程序,我試圖實現一個自定義的標識和主體類。在MVC中實現一個自定義標識和IPrincipal

我創建了實現IIdentity和IPrincipal接口的類,實例化它們,然後在Global.asax的Application_AuthenticateRequest中將CustomPrincipal對象分配給Context.User。

這一切都成功,對象看起來不錯。當我開始渲染視圖時,頁面現在失敗。第一次失敗是在下面的代碼行默認LogoOnUserControl觀點:

[ <%= Html.ActionLink("Log Off", "LogOff", "Account") %> ] 

如果我拉了這一點,它失敗,那麼在代碼不同的「Html.ActionLink」行。

我收到的錯誤是:

類型的異常 'System.Runtime.Serialization.SerializationException' 發生在WebDev.WebHost40.dll但 不是在用戶代碼來處理

附加信息:類型不是 已解決會員 'Model.Entities.UserIdentity,Model, Version = 1.0.0.0,Culture = neutral, PublicKeyToken = null'。

是否有一些額外的屬性需要在我的標識中實現才能在MVC中使用自定義標識?我試圖在Identity類中實現[Serializable()],但它似乎沒有影響。

更新: 我已經嘗試過3-4種替代方法來實現這一點,但仍然失敗,同樣的錯誤。如果我直接使用GenericIdentity/GenericPrincipal類,它不會報錯。

GenericIdentity ident = new GenericIdentity("jzxcvcx"); 
GenericPrincipal princ = new GenericPrincipal(ident, null); 
Context.User = princ; 

但是這讓我無處可去,因爲我試圖使用CustomIdentity來保存幾個屬性。如果我實現了IIdentity/IPrincipal接口或繼承了我的CustomIdentity/CustomPrincipal的GenericIdentity/GenericPrincipal,它將失敗,並顯示上面的原始錯誤。

+0

回覆:向上投...你也看到了類似的問題? – Jay 2009-12-11 13:21:59

回答

16

我想到了這一點,從網上的一些幫助:)訣竅是,你必須在你的類實現ISerializable接口,實現IIdentity。我希望這有助於挽救別人的一段時間:)

類聲明:

[Serializable] 
    public class ForumUserIdentity : IIdentity, ISerializable 

執行情況ISerializable的:

#region ISerializable Members 

     public void GetObjectData(SerializationInfo info, StreamingContext context) 
     { 
      if (context.State == StreamingContextStates.CrossAppDomain) 
      { 
       GenericIdentity gIdent = new GenericIdentity(this.Name, this.AuthenticationType); 
       info.SetType(gIdent.GetType()); 

       System.Reflection.MemberInfo[] serializableMembers; 
       object[] serializableValues; 

       serializableMembers = FormatterServices.GetSerializableMembers(gIdent.GetType()); 
       serializableValues = FormatterServices.GetObjectData(gIdent, serializableMembers); 

       for (int i = 0; i < serializableMembers.Length; i++) 
       { 
        info.AddValue(serializableMembers[i].Name, serializableValues[i]); 
       } 
      } 
      else 
      { 
       throw new InvalidOperationException("Serialization not supported"); 
      } 
     } 

     #endregion 

這裏是link to the article that has more detail on the "Feature"

+0

這個功能是BS ...自2006年以來一直圍繞着我的asp.net ** mvc-3 **應用程序。 – 2011-01-11 23:02:21

+0

這解決了我的問題。同樣的事情也適用於IPrincipal。 – magnus 2011-01-18 09:28:16

+0

爲什麼您在DeSerialization代碼中創建了一個新的'GenericIdentity'而不是'CustomIdentity'? =>'GenericIdentity gIdent = new GenericIdentity(...)'?? – 2011-03-21 07:43:57

4

與我它似乎在我從繼承我的Identity類時起作用。

另請注意:使用Linq-to-Sql時沒有問題。我切換到實體框架和砰砰聲,我得到了上述消息。

+1

http://stackoverflow.com/questions/6503380/how-expensive-is-using-marshalbyrefobject-compared-to-serialization還有更多marshalbyrefobject – Sergey 2012-01-31 05:18:17

11

我有同樣的問題。我通過將我的主體創建從MvcApplication_AuthenticateRequest移動到MvcApplication_PostAuthenticateRequest來解決它。 我不知道爲什麼/怎麼樣,但是它解決了這個問題:)

 void MvcApplication_PostAuthenticateRequest(object sender, EventArgs e) 
    { 
     HttpCookie authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName]; 
     if (authCookie != null) 
     { 
      string encTicket = authCookie.Value; 
      if (!String.IsNullOrEmpty(encTicket)) 
      { 
       FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(encTicket); 
       BiamedIdentity id = new BiamedIdentity(ticket); 
       GenericPrincipal prin = new GenericPrincipal(id, null); 
       HttpContext.Current.User = prin; 
      } 
     } 
    } 
+0

這一個工程。 – 2011-01-11 23:45:21

相關問題