gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 18:13:08 24 4
gpt4 key购买 nike

该应用程序在 ASP.NET Core 2.1 中具有以下用户和 Controller 。

应用用户

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; }
}
}

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 方法应该返回 View ,并将当前登录的 DifferentUser 传递给它。但它返回以下错误。

InvalidCastException:无法将“MyApp.Models.AppUser”类型的对象转换为类型“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();
}
}
}

最佳答案

为避免强制转换,您可以使用枚举来定义您的用户类型,并在这两种情况下都使用您的 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
}
}

关于c# - 如何在 ASP.NET Core 中定义和使用不同的用户类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53438247/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com