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

问题描述 投票:3回答:1

该应用程序在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);
            }
        }
    }

在DifferentUserController中,Index方法应返回视图,并将当前登录的DifferentUser传递给它。但它返回以下错误。

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

在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
1个回答
0
投票

为避免需要强制转换,您可以使用枚举来定义用户类型,并在两种情况下使用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
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.