How to Define and use Different User Types in ASP.NET Core












3















The application has the following users and controller in ASP.NET Core 2.1.



AppUser



using Microsoft.AspNetCore.Identity;

namespace MyApp.Models
{
public class AppUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
}


DifferentUser



namespace MyApp.Models
{
public class DifferentUser : AppUser
{
public string Property1 { get; set; }
}
}


DifferentUserController.cs



using System.Threading.Tasks;
using MyApp.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;

namespace MyApp.Controllers
{
[Authorize(Roles = "DifferentUser")]
public class DifferentUserController : Controller
{

private UserManager<AppUser> userManager;

public DifferentUserController(UserManager<AppUser> _userManager)
{
userManager = _userManager;
}

[HttpGet]
public async Task<IActionResult> Index()
{
var user = (DifferentUser) await userManager.GetUserAsync(HttpContext.User);
return View("~/Views/DifferentUser/Index.cshtml", user);
}
}
}


Within the DifferentUserController, the Index method should return the view, and pass it the currently logged in DifferentUser. But it returns the following error.



InvalidCastException: Unable to cast object of type 'MyApp.Models.AppUser' to type 'MyApp.Models.DifferentUser'.



What is the correct/best way of handling different user types in ASP.NET Core 2.1?



Startup.cs



using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using MyApp.Models;
using MyApp.Infrastructure;
using Microsoft.AspNetCore.Identity;

namespace MyApp
{
public class Startup
{

public Startup(IConfiguration configuration) =>
Configuration = configuration;

public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IPasswordValidator<AppUser>,
CustomPasswordValidator>();

services.AddTransient<IUserValidator<AppUser>,
CustomUserValidator>();

services.AddDbContext<AppIdentityDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

services.AddIdentity<AppUser, IdentityRole>(opts => {
opts.User.RequireUniqueEmail = true;
opts.Password.RequiredLength = 6;
opts.Password.RequireNonAlphanumeric = false;
opts.Password.RequireLowercase = false;
opts.Password.RequireUppercase = false;
opts.Password.RequireDigit = false;
}).AddEntityFrameworkStores<AppIdentityDbContext>()
.AddDefaultTokenProviders();

services.AddMvc();
}

public void Configure(IApplicationBuilder app)
{
app.UseStatusCodePages();
app.UseDeveloperExceptionPage();
app.UseStaticFiles();
app.UseAuthentication();
app.UseMvcWithDefaultRoute();
}
}
}









share|improve this question

























  • UserManager<AppUser> expects an AppUser but wont be able to cast because it is unaware of DifferentUser in this context. If you use UserManager<DifferentUser> it should be able to provide the desired behavior.

    – Nkosi
    Nov 22 '18 at 22:14













  • The trick here would be to make sure the necessary generics are registered with the IoC container so that the correct type is injected into the controller.

    – Nkosi
    Nov 22 '18 at 22:16











  • Tried that: InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager`1[MyApp.Models.DifferentUser]' while attempting to activate 'MyApp.Controllers.DifferentUserController'.

    – crayden
    Nov 22 '18 at 22:30











  • did you register the appropriate manager with the container?

    – Nkosi
    Nov 22 '18 at 22:38











  • I used UserManager<DifferentUser>. Is that correct?

    – crayden
    Nov 22 '18 at 22:40
















3















The application has the following users and controller in ASP.NET Core 2.1.



AppUser



using Microsoft.AspNetCore.Identity;

namespace MyApp.Models
{
public class AppUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
}


DifferentUser



namespace MyApp.Models
{
public class DifferentUser : AppUser
{
public string Property1 { get; set; }
}
}


DifferentUserController.cs



using System.Threading.Tasks;
using MyApp.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;

namespace MyApp.Controllers
{
[Authorize(Roles = "DifferentUser")]
public class DifferentUserController : Controller
{

private UserManager<AppUser> userManager;

public DifferentUserController(UserManager<AppUser> _userManager)
{
userManager = _userManager;
}

[HttpGet]
public async Task<IActionResult> Index()
{
var user = (DifferentUser) await userManager.GetUserAsync(HttpContext.User);
return View("~/Views/DifferentUser/Index.cshtml", user);
}
}
}


Within the DifferentUserController, the Index method should return the view, and pass it the currently logged in DifferentUser. But it returns the following error.



InvalidCastException: Unable to cast object of type 'MyApp.Models.AppUser' to type 'MyApp.Models.DifferentUser'.



What is the correct/best way of handling different user types in ASP.NET Core 2.1?



Startup.cs



using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using MyApp.Models;
using MyApp.Infrastructure;
using Microsoft.AspNetCore.Identity;

namespace MyApp
{
public class Startup
{

public Startup(IConfiguration configuration) =>
Configuration = configuration;

public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IPasswordValidator<AppUser>,
CustomPasswordValidator>();

services.AddTransient<IUserValidator<AppUser>,
CustomUserValidator>();

services.AddDbContext<AppIdentityDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

services.AddIdentity<AppUser, IdentityRole>(opts => {
opts.User.RequireUniqueEmail = true;
opts.Password.RequiredLength = 6;
opts.Password.RequireNonAlphanumeric = false;
opts.Password.RequireLowercase = false;
opts.Password.RequireUppercase = false;
opts.Password.RequireDigit = false;
}).AddEntityFrameworkStores<AppIdentityDbContext>()
.AddDefaultTokenProviders();

services.AddMvc();
}

public void Configure(IApplicationBuilder app)
{
app.UseStatusCodePages();
app.UseDeveloperExceptionPage();
app.UseStaticFiles();
app.UseAuthentication();
app.UseMvcWithDefaultRoute();
}
}
}









share|improve this question

























  • UserManager<AppUser> expects an AppUser but wont be able to cast because it is unaware of DifferentUser in this context. If you use UserManager<DifferentUser> it should be able to provide the desired behavior.

    – Nkosi
    Nov 22 '18 at 22:14













  • The trick here would be to make sure the necessary generics are registered with the IoC container so that the correct type is injected into the controller.

    – Nkosi
    Nov 22 '18 at 22:16











  • Tried that: InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager`1[MyApp.Models.DifferentUser]' while attempting to activate 'MyApp.Controllers.DifferentUserController'.

    – crayden
    Nov 22 '18 at 22:30











  • did you register the appropriate manager with the container?

    – Nkosi
    Nov 22 '18 at 22:38











  • I used UserManager<DifferentUser>. Is that correct?

    – crayden
    Nov 22 '18 at 22:40














3












3








3


1






The application has the following users and controller in ASP.NET Core 2.1.



AppUser



using Microsoft.AspNetCore.Identity;

namespace MyApp.Models
{
public class AppUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
}


DifferentUser



namespace MyApp.Models
{
public class DifferentUser : AppUser
{
public string Property1 { get; set; }
}
}


DifferentUserController.cs



using System.Threading.Tasks;
using MyApp.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;

namespace MyApp.Controllers
{
[Authorize(Roles = "DifferentUser")]
public class DifferentUserController : Controller
{

private UserManager<AppUser> userManager;

public DifferentUserController(UserManager<AppUser> _userManager)
{
userManager = _userManager;
}

[HttpGet]
public async Task<IActionResult> Index()
{
var user = (DifferentUser) await userManager.GetUserAsync(HttpContext.User);
return View("~/Views/DifferentUser/Index.cshtml", user);
}
}
}


Within the DifferentUserController, the Index method should return the view, and pass it the currently logged in DifferentUser. But it returns the following error.



InvalidCastException: Unable to cast object of type 'MyApp.Models.AppUser' to type 'MyApp.Models.DifferentUser'.



What is the correct/best way of handling different user types in ASP.NET Core 2.1?



Startup.cs



using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using MyApp.Models;
using MyApp.Infrastructure;
using Microsoft.AspNetCore.Identity;

namespace MyApp
{
public class Startup
{

public Startup(IConfiguration configuration) =>
Configuration = configuration;

public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IPasswordValidator<AppUser>,
CustomPasswordValidator>();

services.AddTransient<IUserValidator<AppUser>,
CustomUserValidator>();

services.AddDbContext<AppIdentityDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

services.AddIdentity<AppUser, IdentityRole>(opts => {
opts.User.RequireUniqueEmail = true;
opts.Password.RequiredLength = 6;
opts.Password.RequireNonAlphanumeric = false;
opts.Password.RequireLowercase = false;
opts.Password.RequireUppercase = false;
opts.Password.RequireDigit = false;
}).AddEntityFrameworkStores<AppIdentityDbContext>()
.AddDefaultTokenProviders();

services.AddMvc();
}

public void Configure(IApplicationBuilder app)
{
app.UseStatusCodePages();
app.UseDeveloperExceptionPage();
app.UseStaticFiles();
app.UseAuthentication();
app.UseMvcWithDefaultRoute();
}
}
}









share|improve this question
















The application has the following users and controller in ASP.NET Core 2.1.



AppUser



using Microsoft.AspNetCore.Identity;

namespace MyApp.Models
{
public class AppUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
}


DifferentUser



namespace MyApp.Models
{
public class DifferentUser : AppUser
{
public string Property1 { get; set; }
}
}


DifferentUserController.cs



using System.Threading.Tasks;
using MyApp.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;

namespace MyApp.Controllers
{
[Authorize(Roles = "DifferentUser")]
public class DifferentUserController : Controller
{

private UserManager<AppUser> userManager;

public DifferentUserController(UserManager<AppUser> _userManager)
{
userManager = _userManager;
}

[HttpGet]
public async Task<IActionResult> Index()
{
var user = (DifferentUser) await userManager.GetUserAsync(HttpContext.User);
return View("~/Views/DifferentUser/Index.cshtml", user);
}
}
}


Within the DifferentUserController, the Index method should return the view, and pass it the currently logged in DifferentUser. But it returns the following error.



InvalidCastException: Unable to cast object of type 'MyApp.Models.AppUser' to type 'MyApp.Models.DifferentUser'.



What is the correct/best way of handling different user types in ASP.NET Core 2.1?



Startup.cs



using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using MyApp.Models;
using MyApp.Infrastructure;
using Microsoft.AspNetCore.Identity;

namespace MyApp
{
public class Startup
{

public Startup(IConfiguration configuration) =>
Configuration = configuration;

public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IPasswordValidator<AppUser>,
CustomPasswordValidator>();

services.AddTransient<IUserValidator<AppUser>,
CustomUserValidator>();

services.AddDbContext<AppIdentityDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

services.AddIdentity<AppUser, IdentityRole>(opts => {
opts.User.RequireUniqueEmail = true;
opts.Password.RequiredLength = 6;
opts.Password.RequireNonAlphanumeric = false;
opts.Password.RequireLowercase = false;
opts.Password.RequireUppercase = false;
opts.Password.RequireDigit = false;
}).AddEntityFrameworkStores<AppIdentityDbContext>()
.AddDefaultTokenProviders();

services.AddMvc();
}

public void Configure(IApplicationBuilder app)
{
app.UseStatusCodePages();
app.UseDeveloperExceptionPage();
app.UseStaticFiles();
app.UseAuthentication();
app.UseMvcWithDefaultRoute();
}
}
}






c# asp.net-core asp.net-identity






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 22:50







crayden

















asked Nov 22 '18 at 21:38









craydencrayden

3771421




3771421













  • UserManager<AppUser> expects an AppUser but wont be able to cast because it is unaware of DifferentUser in this context. If you use UserManager<DifferentUser> it should be able to provide the desired behavior.

    – Nkosi
    Nov 22 '18 at 22:14













  • The trick here would be to make sure the necessary generics are registered with the IoC container so that the correct type is injected into the controller.

    – Nkosi
    Nov 22 '18 at 22:16











  • Tried that: InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager`1[MyApp.Models.DifferentUser]' while attempting to activate 'MyApp.Controllers.DifferentUserController'.

    – crayden
    Nov 22 '18 at 22:30











  • did you register the appropriate manager with the container?

    – Nkosi
    Nov 22 '18 at 22:38











  • I used UserManager<DifferentUser>. Is that correct?

    – crayden
    Nov 22 '18 at 22:40



















  • UserManager<AppUser> expects an AppUser but wont be able to cast because it is unaware of DifferentUser in this context. If you use UserManager<DifferentUser> it should be able to provide the desired behavior.

    – Nkosi
    Nov 22 '18 at 22:14













  • The trick here would be to make sure the necessary generics are registered with the IoC container so that the correct type is injected into the controller.

    – Nkosi
    Nov 22 '18 at 22:16











  • Tried that: InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager`1[MyApp.Models.DifferentUser]' while attempting to activate 'MyApp.Controllers.DifferentUserController'.

    – crayden
    Nov 22 '18 at 22:30











  • did you register the appropriate manager with the container?

    – Nkosi
    Nov 22 '18 at 22:38











  • I used UserManager<DifferentUser>. Is that correct?

    – crayden
    Nov 22 '18 at 22:40

















UserManager<AppUser> expects an AppUser but wont be able to cast because it is unaware of DifferentUser in this context. If you use UserManager<DifferentUser> it should be able to provide the desired behavior.

– Nkosi
Nov 22 '18 at 22:14







UserManager<AppUser> expects an AppUser but wont be able to cast because it is unaware of DifferentUser in this context. If you use UserManager<DifferentUser> it should be able to provide the desired behavior.

– Nkosi
Nov 22 '18 at 22:14















The trick here would be to make sure the necessary generics are registered with the IoC container so that the correct type is injected into the controller.

– Nkosi
Nov 22 '18 at 22:16





The trick here would be to make sure the necessary generics are registered with the IoC container so that the correct type is injected into the controller.

– Nkosi
Nov 22 '18 at 22:16













Tried that: InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager`1[MyApp.Models.DifferentUser]' while attempting to activate 'MyApp.Controllers.DifferentUserController'.

– crayden
Nov 22 '18 at 22:30





Tried that: InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager`1[MyApp.Models.DifferentUser]' while attempting to activate 'MyApp.Controllers.DifferentUserController'.

– crayden
Nov 22 '18 at 22:30













did you register the appropriate manager with the container?

– Nkosi
Nov 22 '18 at 22:38





did you register the appropriate manager with the container?

– Nkosi
Nov 22 '18 at 22:38













I used UserManager<DifferentUser>. Is that correct?

– crayden
Nov 22 '18 at 22:40





I used UserManager<DifferentUser>. Is that correct?

– crayden
Nov 22 '18 at 22:40












1 Answer
1






active

oldest

votes


















0














To avoid the need to cast, you could use an enum to define your user type and use your AppUser class in both cases. This will make it possible in this scenario.



public class AppUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public UserType Type { get; set; }

public enum UserType {
User,
OtherUser
}
}





share|improve this answer
























  • Not sure this will work because I will have many different user types, each with many unique properties.

    – crayden
    Nov 22 '18 at 21:58











  • Have you tried interfaces?

    – Marius
    Nov 22 '18 at 22:00











  • No. Just extending the AppUser class. I have also read about Claims based approaches, but I am not familiar with that approach.

    – crayden
    Nov 22 '18 at 22:01






  • 1





    I'd say you need to look at your structure and make sure it isn't too complex. We separate our users from permissions and groups which drive what they can, and cannot see.

    – Marius
    Nov 22 '18 at 22:34






  • 1





    Same as in the comments, I would use normal roles with the same user class type and an enum for the "user kind", then by role i could try to get the extra data depending on the roles. Using inheritance is really a bad idea, inheritance should be used when the Liskov Substitution Principle applies, if not, composition will be best.

    – rekiem87
    Nov 23 '18 at 0:19











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438247%2fhow-to-define-and-use-different-user-types-in-asp-net-core%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














To avoid the need to cast, you could use an enum to define your user type and use your AppUser class in both cases. This will make it possible in this scenario.



public class AppUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public UserType Type { get; set; }

public enum UserType {
User,
OtherUser
}
}





share|improve this answer
























  • Not sure this will work because I will have many different user types, each with many unique properties.

    – crayden
    Nov 22 '18 at 21:58











  • Have you tried interfaces?

    – Marius
    Nov 22 '18 at 22:00











  • No. Just extending the AppUser class. I have also read about Claims based approaches, but I am not familiar with that approach.

    – crayden
    Nov 22 '18 at 22:01






  • 1





    I'd say you need to look at your structure and make sure it isn't too complex. We separate our users from permissions and groups which drive what they can, and cannot see.

    – Marius
    Nov 22 '18 at 22:34






  • 1





    Same as in the comments, I would use normal roles with the same user class type and an enum for the "user kind", then by role i could try to get the extra data depending on the roles. Using inheritance is really a bad idea, inheritance should be used when the Liskov Substitution Principle applies, if not, composition will be best.

    – rekiem87
    Nov 23 '18 at 0:19
















0














To avoid the need to cast, you could use an enum to define your user type and use your AppUser class in both cases. This will make it possible in this scenario.



public class AppUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public UserType Type { get; set; }

public enum UserType {
User,
OtherUser
}
}





share|improve this answer
























  • Not sure this will work because I will have many different user types, each with many unique properties.

    – crayden
    Nov 22 '18 at 21:58











  • Have you tried interfaces?

    – Marius
    Nov 22 '18 at 22:00











  • No. Just extending the AppUser class. I have also read about Claims based approaches, but I am not familiar with that approach.

    – crayden
    Nov 22 '18 at 22:01






  • 1





    I'd say you need to look at your structure and make sure it isn't too complex. We separate our users from permissions and groups which drive what they can, and cannot see.

    – Marius
    Nov 22 '18 at 22:34






  • 1





    Same as in the comments, I would use normal roles with the same user class type and an enum for the "user kind", then by role i could try to get the extra data depending on the roles. Using inheritance is really a bad idea, inheritance should be used when the Liskov Substitution Principle applies, if not, composition will be best.

    – rekiem87
    Nov 23 '18 at 0:19














0












0








0







To avoid the need to cast, you could use an enum to define your user type and use your AppUser class in both cases. This will make it possible in this scenario.



public class AppUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public UserType Type { get; set; }

public enum UserType {
User,
OtherUser
}
}





share|improve this answer













To avoid the need to cast, you could use an enum to define your user type and use your AppUser class in both cases. This will make it possible in this scenario.



public class AppUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public UserType Type { get; set; }

public enum UserType {
User,
OtherUser
}
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 21:55









MariusMarius

867




867













  • Not sure this will work because I will have many different user types, each with many unique properties.

    – crayden
    Nov 22 '18 at 21:58











  • Have you tried interfaces?

    – Marius
    Nov 22 '18 at 22:00











  • No. Just extending the AppUser class. I have also read about Claims based approaches, but I am not familiar with that approach.

    – crayden
    Nov 22 '18 at 22:01






  • 1





    I'd say you need to look at your structure and make sure it isn't too complex. We separate our users from permissions and groups which drive what they can, and cannot see.

    – Marius
    Nov 22 '18 at 22:34






  • 1





    Same as in the comments, I would use normal roles with the same user class type and an enum for the "user kind", then by role i could try to get the extra data depending on the roles. Using inheritance is really a bad idea, inheritance should be used when the Liskov Substitution Principle applies, if not, composition will be best.

    – rekiem87
    Nov 23 '18 at 0:19



















  • Not sure this will work because I will have many different user types, each with many unique properties.

    – crayden
    Nov 22 '18 at 21:58











  • Have you tried interfaces?

    – Marius
    Nov 22 '18 at 22:00











  • No. Just extending the AppUser class. I have also read about Claims based approaches, but I am not familiar with that approach.

    – crayden
    Nov 22 '18 at 22:01






  • 1





    I'd say you need to look at your structure and make sure it isn't too complex. We separate our users from permissions and groups which drive what they can, and cannot see.

    – Marius
    Nov 22 '18 at 22:34






  • 1





    Same as in the comments, I would use normal roles with the same user class type and an enum for the "user kind", then by role i could try to get the extra data depending on the roles. Using inheritance is really a bad idea, inheritance should be used when the Liskov Substitution Principle applies, if not, composition will be best.

    – rekiem87
    Nov 23 '18 at 0:19

















Not sure this will work because I will have many different user types, each with many unique properties.

– crayden
Nov 22 '18 at 21:58





Not sure this will work because I will have many different user types, each with many unique properties.

– crayden
Nov 22 '18 at 21:58













Have you tried interfaces?

– Marius
Nov 22 '18 at 22:00





Have you tried interfaces?

– Marius
Nov 22 '18 at 22:00













No. Just extending the AppUser class. I have also read about Claims based approaches, but I am not familiar with that approach.

– crayden
Nov 22 '18 at 22:01





No. Just extending the AppUser class. I have also read about Claims based approaches, but I am not familiar with that approach.

– crayden
Nov 22 '18 at 22:01




1




1





I'd say you need to look at your structure and make sure it isn't too complex. We separate our users from permissions and groups which drive what they can, and cannot see.

– Marius
Nov 22 '18 at 22:34





I'd say you need to look at your structure and make sure it isn't too complex. We separate our users from permissions and groups which drive what they can, and cannot see.

– Marius
Nov 22 '18 at 22:34




1




1





Same as in the comments, I would use normal roles with the same user class type and an enum for the "user kind", then by role i could try to get the extra data depending on the roles. Using inheritance is really a bad idea, inheritance should be used when the Liskov Substitution Principle applies, if not, composition will be best.

– rekiem87
Nov 23 '18 at 0:19





Same as in the comments, I would use normal roles with the same user class type and an enum for the "user kind", then by role i could try to get the extra data depending on the roles. Using inheritance is really a bad idea, inheritance should be used when the Liskov Substitution Principle applies, if not, composition will be best.

– rekiem87
Nov 23 '18 at 0:19


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438247%2fhow-to-define-and-use-different-user-types-in-asp-net-core%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Create new schema in PostgreSQL using DBeaver

Deepest pit of an array with Javascript: test on Codility

Costa Masnaga