Skip to content

Instantly share code, notes, and snippets.

@theuntitled
Last active October 26, 2023 17:12
Show Gist options
  • Save theuntitled/7c70fff994993d7644f12d5bb0dc205f to your computer and use it in GitHub Desktop.
Save theuntitled/7c70fff994993d7644f12d5bb0dc205f to your computer and use it in GitHub Desktop.
Ignoring "Roles", "Claims" and "Logins" when serializing an ApplicationUser / IdentityUser
using System;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNet.Identity.EntityFramework;
using Newtonsoft.Json;
namespace Project.Models
{
[MetadataType(typeof (ApplicationUserMetaData))]
public class ApplicationUser : IdentityUser
{
// ...
#region overrides
public override string Email { get; set; }
[JsonIgnore]
public override bool EmailConfirmed { get; set; }
[JsonIgnore]
public override bool TwoFactorEnabled { get; set; }
[JsonIgnore]
public override string PhoneNumber { get; set; }
[JsonIgnore]
public override bool PhoneNumberConfirmed { get; set; }
[JsonIgnore]
public override string PasswordHash { get; set; }
[JsonIgnore]
public override string SecurityStamp { get; set; }
[JsonIgnore]
public override bool LockoutEnabled { get; set; }
[JsonIgnore]
public override DateTime? LockoutEndDateUtc { get; set; }
[JsonIgnore]
public override int AccessFailedCount { get; set; }
#endregion
}
}
using System.Collections.Generic;
using Microsoft.AspNet.Identity.EntityFramework;
using Newtonsoft.Json;
namespace Project.Models
{
public class ApplicationUserMetaData
{
[JsonIgnore]
public ICollection<IdentityUserClaim> Claims { get; }
[JsonIgnore]
public ICollection<IdentityUserLogin> Logins { get; }
[JsonIgnore]
public ICollection<IdentityUserRole> Roles { get; }
}
}
@Feargalicious
Copy link

<3

@mfuzailzubari
Copy link

Thanks for sharing this.

@workjoule
Copy link

Years go by ... \o/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment