I am implementing ASP.NET Identity in a system where used authentication with a User Model with Entity Framework and being controlled in WebApp by Session (yes it is a legacy system into web forms), we implemented, replaced the User
model by ApplicationUser
of Identity, everything working OK.
The problem is, there is a notification system, which is basically a Many to Many relationship between a Notification
and User
Model,
these notifications are saved in SQL Server along with the rest of the system, but are also in a Cache Redis for faster reading and needs to be serialized for writing on it. We remove the User
model, then added an ICollection
of ApplicationUser
in the Notification
model and vice versa - making the relate between both.
But ApplicationUser
inherits from IdentityUser
, and even adding the annotation [Serializable]
I get the exception:
Type 'Microsoft.AspNet.Identity.EntityFramework.IdentityUser' the assembly 'Microsoft.AspNet.Identity.EntityFramework, Version = 2.0.0.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35' is not marked as serializable
My question is, is there any way to serialize this? Or I will have to create another user model only to relate with notifications model?
ApplicationUser
Model
[Serializable]
public class ApplicationUser : IdentityUser
{
public ApplicationUser()
{
this.Notificacoes = new List<Notificacao>();
}
public ClaimsIdentity GenerateUserIdentity(IdentityConfig.ApplicationUserManager manager)
{
var userIdentity = manager.CreateIdentity(this, DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}
public Task<ClaimsIdentity> GenerateUserIdentityAsync(IdentityConfig.ApplicationUserManager manager)
{
return Task.FromResult(GenerateUserIdentity(manager));
}
public bool ReceiveNotifications { get; set; }
public int Permission { get; set; }
public virtual ICollection<Notificacao> Notificacoes { get; set; }
}
Notification
Model
[Serializable]
public partial class Notificacao
{
public Notificacao()
{
this.Usuarios = new List<ApplicationUser>();
}
public int Codigo { get; set; }
public string Mensagem { get; set; }
public DateTime DataHoraNotificacao { get; set; }
public int Tipo { get; set; }
public virtual ICollection<ApplicationUser> Usuarios { get; set; }
}
Serialize Method used to Serialize a object to Redis (where it throws the exception)
static byte[] Serialize(object o)
{
if (o == null)
{
return null;
}
BinaryFormatter binaryFormatter = new BinaryFormatter();
using (MemoryStream memoryStream = new MemoryStream())
{
binaryFormatter.Serialize(memoryStream, o);
byte[] objectDataAsStream = memoryStream.ToArray();
return objectDataAsStream;
}
}