C# 如何在ASP.NET Core中定义和使用不同的用户类型

C# 如何在ASP.NET Core中定义和使用不同的用户类型,c#,asp.net-core,asp.net-identity,C#,Asp.net Core,Asp.net Identity,该应用程序在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; } } }

该应用程序在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; }
    }
}
namespace MyApp.Models
{
    public class DifferentUser : AppUser
    {
        public string Property1 { get; set; }
    }
}
差异化者

using Microsoft.AspNetCore.Identity;

namespace MyApp.Models
{
    public class AppUser : IdentityUser
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}
namespace MyApp.Models
{
    public class DifferentUser : AppUser
    {
        public string Property1 { get; set; }
    }
}
差异控制器.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);
            }
        }
    }
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();
        }
    }
}

为了避免强制转换的需要,可以使用枚举定义用户类型,并在这两种情况下使用AppUser类。这将使这种情况成为可能

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

    public enum UserType {
        User,
        OtherUser
    }
}

我不确定这是否有效,因为我将有许多不同的用户类型,每个类型都有许多独特的属性。否。只是扩展AppUser类。我也读过关于基于声明的方法,但我不熟悉这种方法。简要解释一下为什么有这么多不同的用户类型?我想说,你需要看看你的结构,确保它不太复杂。我们将用户从权限和组中分离出来,这些权限和组驱动他们可以看到的和看不到的内容。与评论中相同,我将使用具有相同用户类类型的普通角色和“用户类型”的枚举,然后通过角色,我可以根据角色尝试获取额外的数据。使用继承确实是一个坏主意,当Liskov替换原则适用时应该使用继承,如果不适用,组合将是最好的。
UserManager
需要一个
AppUser
,但无法强制转换,因为在这种情况下它不知道
differentituser
。如果您使用
UserManager
,它应该能够提供所需的行为。这里的技巧是确保在IoC容器中注册必要的泛型,以便将正确的类型注入控制器。尝试了:InvalidOperationException:无法解析类型的服务“Microsoft.AspNetCore.Identity.UserManager`1[MyApp.Models.Differentituser]”试图激活“MyApp.Controller.DifferentituserController”。是否向容器注册了相应的管理器?我使用了UserManager。对吗?