首页 > 编程语言 >造轮子之asp.net core identity

造轮子之asp.net core identity

时间:2023-10-09 11:25:15浏览次数:33  
标签:core asp builder Role net Property HasMaxLength public identity

在前面我们完成了应用最基础的功能支持以及数据库配置,接下来就是我们的用户角色登录等功能了,在asp.net core中原生Identity可以让我们快速完成这个功能的开发,在.NET8中,asp.net core identity支持了WebApi的注册登录。这让我们在WebApi中可以更爽快的使用。

安装包

首先我们需要安装Microsoft.AspNetCore.Identity.EntityFrameworkCore这个包来创建我们的数据库结构

创建实体

在asp.net core identity中默认包含了IdentityUser,IdentityRole,IdentityRoleClaim,IdentityUserClaim,IdentityUserLogin,IdentityUserRole,IdentityUserToken这几个基类,我们可以直接使用这些,也可以通过继承来灵活扩展我们的表结构。当然,可以按照约定不使用继承的方式,创建类添加必要的属性字段也可。
这里我们选择把所有的类都继承一遍,方便以后扩展。

namespace Wheel.Domain.Identity
{
    public class User : IdentityUser, IEntity<string>
    {
        public virtual DateTimeOffset CreationTime { get; set; }
        public virtual ICollection<UserClaim> Claims { get; set; }
        public virtual ICollection<UserLogin> Logins { get; set; }
        public virtual ICollection<UserToken> Tokens { get; set; }
        public virtual ICollection<UserRole>? UserRoles { get; set; }
    }
}
namespace Wheel.Domain.Identity
{
    public class Role : IdentityRole, IEntity<string>
    {
        /// <summary>
        /// 角色类型,0管理台角色,1客户端角色
        /// </summary>
        public RoleType RoleType { get; set; }

        public Role(string roleName, RoleType roleType) : base (roleName)
        {
            RoleType = roleType;
        }

        public Role(string roleName) : base (roleName)
        {
        }

        public Role() : base ()
        {
        }

        public virtual ICollection<UserRole> UserRoles { get; set; }
        public virtual ICollection<RoleClaim> RoleClaims { get; set; }
    }
}

这里主要展示一下User和Role,别的可自行查看代码仓库。

修改DbContext

在WheelDbContext继承IdentityDbContext,IdentityDbContext支持传入我们的泛型User,Role类型。

namespace Wheel.EntityFrameworkCore
{
    public class WheelDbContext : IdentityDbContext<User, Role, string, UserClaim, UserRole, UserLogin, RoleClaim, UserToken>
    {
        private StoreOptions? GetStoreOptions() => this.GetService<IDbContextOptions>()
                            .Extensions.OfType<CoreOptionsExtension>()
                            .FirstOrDefault()?.ApplicationServiceProvider
                            ?.GetService<IOptions<IdentityOptions>>()
                            ?.Value?.Stores;

        public WheelDbContext(DbContextOptions<WheelDbContext> options) : base(options)
        {

        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            ConfigureIdentity(builder);
        }

        void ConfigureIdentity(ModelBuilder builder)
        {
            var storeOptions = GetStoreOptions();
            var maxKeyLength = storeOptions?.MaxLengthForKeys ?? 0;

            builder.Entity<User>(b =>
            {
                b.HasKey(u => u.Id);
                b.HasIndex(u => u.NormalizedUserName).HasDatabaseName("UserNameIndex").IsUnique();
                b.HasIndex(u => u.NormalizedEmail).HasDatabaseName("EmailIndex");
                b.ToTable("Users");
                b.Property(u => u.ConcurrencyStamp).IsConcurrencyToken();

                b.Property(u => u.Id).HasMaxLength(36);
                b.Property(u => u.UserName).HasMaxLength(256);
                b.Property(u => u.NormalizedUserName).HasMaxLength(256);
                b.Property(u => u.Email).HasMaxLength(256);
                b.Property(u => u.NormalizedEmail).HasMaxLength(256);
                b.Property(u => u.CreationTime).HasDefaultValue(DateTimeOffset.Now);

                b.HasMany(e => e.Claims)
                    .WithOne(e => e.User)
                    .HasForeignKey(uc => uc.UserId)
                    .IsRequired();

                b.HasMany(e => e.Logins)
                    .WithOne(e => e.User)
                    .HasForeignKey(ul => ul.UserId)
                    .IsRequired();

                b.HasMany(e => e.Tokens)
                    .WithOne(e => e.User)
                    .HasForeignKey(ut => ut.UserId)
                    .IsRequired();

                b.HasMany(e => e.UserRoles)
                    .WithOne(e => e.User)
                    .HasForeignKey(ur => ur.UserId)
                    .IsRequired();
            });
            builder.Entity<UserClaim>(b =>
            {
                b.HasKey(uc => uc.Id);
                b.ToTable("UserClaims");
            });
            builder.Entity<UserLogin>(b =>
            {
                b.HasKey(l => new { l.LoginProvider, l.ProviderKey });

                if (maxKeyLength > 0)
                {
                    b.Property(l => l.LoginProvider).HasMaxLength(maxKeyLength);
                    b.Property(l => l.ProviderKey).HasMaxLength(maxKeyLength);
                }

                b.ToTable("UserLogins");
            });
            builder.Entity<UserToken>(b =>
            {
                b.HasKey(t => new { t.UserId, t.LoginProvider, t.Name });

                if (maxKeyLength > 0)
                {
                    b.Property(t => t.LoginProvider).HasMaxLength(maxKeyLength);
                    b.Property(t => t.Name).HasMaxLength(maxKeyLength);
                }
                b.ToTable("UserTokens");
            });
            builder.Entity<Role>(b =>
            {
                b.HasKey(r => r.Id);
                b.HasIndex(r => r.NormalizedName).HasDatabaseName("RoleNameIndex").IsUnique();
                b.ToTable("Roles");
                b.Property(u => u.Id).HasMaxLength(36);
                b.Property(r => r.ConcurrencyStamp).IsConcurrencyToken();

                b.Property(u => u.Name).HasMaxLength(256);
                b.Property(u => u.NormalizedName).HasMaxLength(256);

                b.HasMany(e => e.UserRoles)
                    .WithOne(e => e.Role)
                    .HasForeignKey(ur => ur.RoleId)
                    .IsRequired();

                b.HasMany(e => e.RoleClaims)
                    .WithOne(e => e.Role)
                    .HasForeignKey(rc => rc.RoleId)
                    .IsRequired();
            });

            builder.Entity<RoleClaim>(b =>
            {
                b.HasKey(rc => rc.Id);
                b.ToTable("RoleClaims");
            });

            builder.Entity<UserRole>(b =>
            {
                b.HasKey(r => new { r.UserId, r.RoleId });
                b.ToTable("UserRoles");
            });
        }
    }
}

执行数据库迁移命令

接下来我们使用VS的程序包管理器控制台。
使用命令创建和执行迁移文件:

Add-Migration Init
Update-Database

这里也可以使用Dotnet EF命令:

dotnet ef migrations add Init
dotnet ef database update

执行完命令后我们连接数据库即可看到表成功创建。
image.png

配置Identity

在Program中添加下面代码:

builder.Services.AddIdentityCore<User>()
                .AddRoles<Role>()
                .AddEntityFrameworkStores<WheelDbContext>()
                .AddApiEndpoints();

这里指定了Identity用户类型以及角色类型,并且指定EF操作的DbContext。
AddApiEndpoints则是注入WebAPI所需的服务,我们F12进去可以看到里面的配置。

/// <summary>
/// Adds configuration and services needed to support <see cref="IdentityApiEndpointRouteBuilderExtensions.MapIdentityApi{TUser}(IEndpointRouteBuilder)"/>
/// but does not configure authentication. Call <see cref="BearerTokenExtensions.AddBearerToken(AuthenticationBuilder, Action{BearerTokenOptions}?)"/> and/or
/// <see cref="IdentityCookieAuthenticationBuilderExtensions.AddIdentityCookies(AuthenticationBuilder)"/> to configure authentication separately.
/// </summary>
/// <param name="builder">The <see cref="IdentityBuilder"/>.</param>
/// <returns>The <see cref="IdentityBuilder"/>.</returns>
public static IdentityBuilder AddApiEndpoints(this IdentityBuilder builder)
{
    ArgumentNullException.ThrowIfNull(builder);

    builder.AddSignInManager();
    builder.AddDefaultTokenProviders();
    builder.Services.TryAddTransient<IEmailSender, NoOpEmailSender>();
    builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IConfigureOptions<JsonOptions>, IdentityEndpointsJsonOptionsSetup>());
    return builder;
}

接下来就是配置API了,在中间件中添加MapIdentityApi:

app.MapGroup("api/identity")
   .WithTags("Identity")
   .MapIdentityApi<User>();

这里需要注意的是,如果不先MapGroup,则我们的请求路径只直接从/开始的,MapGroup("api/identity")则是指定从/api/identity开始。WithTags则是指定我们Swagger生成API的Tag显示名称。
下面两图可以看到区别:
image.png
image.png
直接调用register和login方法即可完成注册登录,这里只贴上一个登录返回的截图,可以看到我们成功拿到了accessToken以及refreshToken。
image.png
使用Post带上token请求/api/identity/manage/info。成功拿到用户信息。
image.png

这样我们就轻轻松松完成了asp.net core identity对WebApi的集成了。

轮子仓库地址https://github.com/Wheel-Framework/Wheel
欢迎进群催更。

image.png

标签:core,asp,builder,Role,net,Property,HasMaxLength,public,identity
From: https://www.cnblogs.com/fanshaoO/p/17751027.html

相关文章

  • netmiko管理cisco交换机
    fromnetmikoimportConnectHandlersw={'device_type':'cisco_ios_telnet','ip':'10.122.77.180','username':'cisco','password':'cisco'}withConnectHandler(**sw)ascon_sw:......
  • NetCore Ocelot 之 Authorization
    Ocelotsupportsclaimsbasedauthorizationwhichisrunpostauthentication.ThismeansifouhavearouteyouwanttoauthorizeyoucanaddthefollowingtoyouRouteconfiguration."RouteClaimsRequirement":{"client_role":......
  • C#1.0--10.0版本发布时间,.NET,VS版本,CRL版本对应关系
    前言:C#的发展已经有20多年的时间了,从2002.02月发布的C#1.0,到2022.11月发布的C#10.0版本,功能也是不断完善和强大,本人从事.NET相关技术栈开发也有15年的时间,从程序员到架构师,一路走来,头发也白了不少_实战中,获取过有国家发明专利,对微服务,分布式高并发,高可用,高伸缩系统有比较丰富的......
  • .Net析构函数再论(CLR源码级的剖析)
    前言碰到一些问题,发觉依旧没有全面了解完全析构函数。本篇继续看下析构函数的一些引申知识。概述析构函数目前发现的总共有三个标记,这里分别一一介绍下。先上一段代码:internalclassProgram:IDisposable{staticvoidMain(string[]args){StreamReader?......
  • vasp5.4.4+vaspkit安装
    vasp用gnu编译安装是最方便的,下面这个教程非常完整好用vasp-GNU注意看下方评论,第7步更改第33行处,需要删掉-L。vaspkit从sourceforge下载vaspkit打开官网后,右键download获取直链用wget下载即可。解压后运行./setup.sh最后vim~/.vaspkit更改赝势文件路径即可。赝势路径......
  • 出错了,[Docker管理器]运行时发生错误!AttributeError: ‘NoneType’ object has no att
    原文链接:https://www.longkui.site/error/attributeerror-nonetype-object-has-no-attribute-co/4707/0.背景宝塔面板调试docker时,无聊一直在按它的”启动“和”重启“然后就报错了:出错了,[Docker管理器]运行时发生错误!AttributeError:'NoneType'objecthasnoattribute'......
  • php java net 开发应用
    一、语言:PHP:PHP产生与1994年,其语法混合了C、Java、Perl和他自创的一些编程语法;PHP是嵌入在HTML中执行的;它也是一种解释性语言。早期的PHP并非完全的面向对象编程语言,到了PHP4以后的版本才开始有了面向对象的概念。PHP主要在大型网站和小型网站,sns,互联网应用方面广泛使用,高......
  • AlexNet论文精读
    AlexNet......
  • Aspera Faspex RCE后渗透利用
    引言本文介绍了常规AsperaFaspex组件RCE后如何利用数据库并登录后台的简单后渗透利用,常见的RCENday例如:CVE-2022-47986。维持权限RCE通过反弹shell命令,获取ncshell,但此时shell并不是完全交互式的,并且维持权限不方便,不利用后续的利用,因此需要部署一个完全交互式shell木马,可......
  • NetCore Ocelot 之 Qos
    QosqualityofserviceOcelotsupportsoneQoscapabilityatthecurrenttime.YoucansetonaperRoutebasisifyouwanttouseacircuitbreakerwhenmakingrequeststoadownstreamservice.Thisusesanawesome.NETlibrarycalledPolly.Thefirstthi......