错误 - 未标记为可序列化

Mad*_* Zu 48 .net c# asp.net serialization

我得到的错误是:

Type 'OrgPermission' in Assembly 'App_Code.ptjvczom, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable. 
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

我有一个gridview,它使用以下DataSource:

 <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetOrgList" 
            TypeName="Org">
    <SelectParameters>
      <asp:SessionParameter Name="orgCodes" SessionField="UserOrgs" Type="Object" />
       <asp:Parameter DefaultValue="Y" Name="active" Type="String" />
    </SelectParameters>
 </asp:ObjectDataSource>
Run Code Online (Sandbox Code Playgroud)

我在页面加载中设置会话变量,如下所示:

User cUser = new User(userid);
//make sure the user is an Admin
List<OrgPermission> orgs = new List<OrgPermission>();
foreach(OrgPermission org in cUser.orgs)
   {
     if (org.type=='admin')
     {
        orgs.Add(org);                       
     }
   }
Session["UserOrgs"] = orgs;
Run Code Online (Sandbox Code Playgroud)

我的用户类看起来像这样:

public class OrgPermission
{
    public string Org { get; set; }   
    public List<string> type { get; set; }

    public OrgPermission()
    { }    
}
public class cUser
{    
    public string userid { get; set; }
    public List<OrgPermission> orgs { get; set; }

    public clsUser(string username)
    {
      //i set everything here
    }
}
Run Code Online (Sandbox Code Playgroud)

我无法理解为什么它会破碎,我可以使用它而不使其可序列化吗?

我尝试调试,会话变量设置得很好,然后进入GetOrgList并返回正确的结果,但页面没有加载,我得到上面的错误.

这是我的GetOrgList函数的片段:

public DataTable GetOrgList(List<OrgPermission> orgCodes, string active)
    {

        string orgList = null;

        //code to set OrgList using the parameter is here.

        DataSet ds = new DataSet();
        SqlConnection conn = new SqlConnection(cCon.getConn());
        SqlCommand cmd = new SqlCommand("sp_GetOrgList", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@orgList", orgList));
        cmd.Parameters.Add(new SqlParameter("@active", active));

            conn.Open();
            SqlDataAdapter sqlDA = new SqlDataAdapter();

            sqlDA.SelectCommand = cmd;
            sqlDA.Fill(ds);

            conn.Close();
        return ds.Tables[0];
    }
Run Code Online (Sandbox Code Playgroud)

bur*_*ION 143

[Serializable]
public class OrgPermission
Run Code Online (Sandbox Code Playgroud)

  • 同样重要的是*为什么*必须添加可序列化标记:放入会话变量的任何对象(除了像int和bools这样的基础对象)必须标记为可序列化.请注意,某些.NET类默认情况下不是 - 即DataView. (22认同)

nim*_*hjm 17

如果将对象存储在会话状态中,则该对象必须是可序列化的.

http://www.hpenterprisesecurity.com/vulncat/en/vulncat/dotnet/asp_dotnet_bad_practices_non_serializable_object_stored_in_session.html


编辑:

为了正确序列化会话,应用程序存储为会话属性的所有对象都必须声明[Serializable]属性.此外,如果对象需要自定义序列化方法,则还必须实现ISerializable接口.

https://vulncat.hpefod.com/en/detail?id=desc.structural.dotnet.asp_dotnet_bad_practices_non_serializable_object_stored_in_session#C%23%2fVB.NET%2fASP.NET

  • 找不到您请求的页面. (2认同)

Jon*_*ono 5

留下我的具体解决方案以实现繁荣,因为这是这个问题的一个棘手版本:

Type 'System.Linq.Enumerable+WhereSelectArrayIterator[T...] was not marked as serializable

由于类具有类型字段,IEnumerable<int>例如:

[Serializable]
class MySessionData{
    public int ID;
    public IEnumerable<int> RelatedIDs; //This can be an issue
}
Run Code Online (Sandbox Code Playgroud)

最初,问题实例是MySessionData从不可序列化列表中设置的:

MySessionData instance = new MySessionData(){ 
    ID = 123,
    RelatedIDs = nonSerizableList.Select<int>(item => item.ID)
};
Run Code Online (Sandbox Code Playgroud)

这里的原因是Select<int>(...)返回的具体类具有不可序列化的类型数据,并且您需要将 id 复制到新的List<int>以解析它。

RelatedIDs = nonSerizableList.Select<int>(item => item.ID).ToList();
Run Code Online (Sandbox Code Playgroud)