Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I need code review for using ASP.NET Identity in a new app.

Goals:

  • Use int instead of GUID for IDs.
  • Separate identity from view layer

I need code review if I did everything right. Yes it is working but maybe I did something that I shouldn't or maybe there is some place for improvement.

ApplicationUser

public class ApplicationUser : IdentityUser<int, CustomUserLogin, CustomUserRole,
    CustomUserClaim>
    {
        public ICollection<ProductCategory> ProductCategories { get; set; }
        public ICollection<Store> Stores { get; set; }
        public ICollection<Product> Products { get; set; }

        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(
    UserManager<ApplicationUser, int> manager)
        {
            var userIdentity = await manager.CreateIdentityAsync(
                this, DefaultAuthenticationTypes.ApplicationCookie);            
            return userIdentity;
        }
    }

    public class CustomUserRole : IdentityUserRole<int> { }
    public class CustomUserClaim : IdentityUserClaim<int> { }
    public class CustomUserLogin : IdentityUserLogin<int> { }

    public class CustomRole : IdentityRole<int, CustomUserRole>
    {
        public CustomRole() { }
        public CustomRole(string name) { Name = name; }
    }

    public class CustomUserStore : UserStore<ApplicationUser, CustomRole, int,
        CustomUserLogin, CustomUserRole, CustomUserClaim>
    {
        public CustomUserStore(ApplicationDbContext context)
            : base(context)
        {
        }
    }

    public class CustomRoleStore : RoleStore<CustomRole, int, CustomUserRole>
    {
        public CustomRoleStore(ApplicationDbContext context)
            : base(context)
        {
        }
    }

DbContext

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, CustomRole,
    int, CustomUserLogin, CustomUserRole, CustomUserClaim>
    {
        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }

        public ApplicationDbContext()
            : base("DefaultConnection")
        {...

View models are still in view layer. All above is in data layer (class library). In the view assembly in AccountController I changed all ApplicationUser to reference ApplicationUser in data layer assembly.

I also added extension to get UserID as int:

public static int GetUserIdInt(this IIdentity identity)
        {
            if (identity == null)
                throw new ArgumentNullException("identity");

            string stringUserId = identity.GetUserId();

            int userId;
            if (string.IsNullOrWhiteSpace(stringUserId) || !int.TryParse(stringUserId, out userId))
            {
                return default(int);
            }

            return userId;
        }
share|improve this question
    
I'm curious about the IdentityUser class, it would be nice if you could include it for context ;) –  Mat's Mug Mar 24 at 19:28
2  
IdentityUser is class from Microsoft.AspNet.Identity.EntityFramework. –  1110 Mar 24 at 19:29
    
Haha can you tell I don't do much web dev! thanks for the clarification :) –  Mat's Mug Mar 24 at 19:32
    
I don't understand? Did I offend you? –  1110 Mar 24 at 19:33
    
Not at all! Sorry for the confusion –  Mat's Mug Mar 24 at 19:34

1 Answer 1

up vote 1 down vote accepted

Your customization looks fine - that's the way it is recommended to do it.

The only note I could add is about your GetUserIdInt method which you can get rid of. You can just use this generic overload:

User.Identity.GetUserId<int>()
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.