Skip to main content
deleted 24 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Using ASP.NET identity - changedin a little bitnew app

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

Goals:
 

  • Use int instead GUID for id's.

    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 ProductCategories { get; set; } public ICollection Stores { get; set; } public ICollection 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)
          {
          }
      }
    
    Separate identity from view layer

DbContextI 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 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.
And 

I also added extension to get UserIDUserID as intint:

Using ASP.NET identity - changed a little bit

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

  • Use int instead GUID for id's.

  • 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 ProductCategories { get; set; } public ICollection Stores { get; set; } public ICollection 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.
And I also added extension to get UserID as int:

Using ASP.NET identity in a new app

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:

Source Link
1110
  • 217
  • 1
  • 2
  • 7

Using ASP.NET identity - changed a little bit

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

  • Use int instead GUID for id's.

  • 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 ProductCategories { get; set; } public ICollection Stores { get; set; } public ICollection 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.
And 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;
        }