.Net6集成IdentityServer4 +AspNetCore Identity读取数据表用户且鉴权授权管理API

 更新时间:2022年07月19日 11:24:56   作者:螺丝起子  
这篇文章主要介绍了.Net6集成IdentityServer4与AspNetCore Identity读取数据表用户且鉴权授权管理API,IdentityServer4 实现鉴权、授权,AspNetCore Identity实现数据库用户管理表直接生成,下文详情需要朋友可以参考一下

前言

IdentityServer4 实现鉴权、授权,AspNetCore Identity实现数据库用户管理表直接生成。

ps:IdentityServer4文档上最后给的例子是 // 配置使用内存存储用户信息,但使用 EF 存储客户端和资源信息,

我初步要实现的是 //数据库存储用户信息   内存存储资源   (下一步资源也放数据库  以后弄好了有机会更)

1.创建.Net6 API程序

一顿引用,包括

防止图片挂掉打一遍文字:

  • IdentityServer4、
  • IdengtityServer4.AspNetIdentity、
  • AspNetCore.Identity.EntityFrameWorkCore(生成数据库表用的)、
  • EntityFrameWork+Disign+Tool三件套 (缺了不能自动迁移)、
  • Pomelo.EntityFrameWorkCore.MySql(我是用的MySql,如果是SqlServer 不用这个用一个大概叫EF.Sqlserver的)、
  • Encrypt (加密MD5用的 不必须)、

下面那个是自带的。

2.建立数据库连接类

using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using MyIDP;
using MyIDP.Models;
using MyIDP.Permission;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

//由此重要
builder.Services.AddDbContext<IdpDbContext>(opt =>
{
    opt.UseMySql("server=127.0.0.1;Port=3306;database=AccountDb;uid=root;pwd=123456;", new MySqlServerVersion(new Version(8,0,29)));
});

builder.Services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddUserManager<MyUserManager>()
                .AddEntityFrameworkStores<IdpDbContext>()
                .AddDefaultTokenProviders();

builder.Services.AddIdentityServer()
    .AddDeveloperSigningCredential()

    .AddInMemoryIdentityResources(MyIDP.IdpConfig.GetIdentityResources())
    .AddInMemoryClients(MyIDP.IdpConfig.GetClients())
    .AddInMemoryApiScopes( MyIDP.IdpConfig.GetScope())
    .AddInMemoryApiResources( MyIDP.IdpConfig.GetApiResources())    //.AddResourceOwnerValidator<MyResourceOwnerPasswordValidator>() //这句可以打开自主验证登录用户
    //.AddProfileService<MyProfileService>()
    .AddAspNetIdentity<ApplicationUser>()
    //.AddTestUsers(new List<IdentityServer4.Test.TestUser>
    //{
    //    new IdentityServer4.Test.TestUser
    //    {
    //        SubjectId="123",
    //        Username = "alice",
    //        Password = "alice",
    //        Claims = new List<Claim>() {
    //            new Claim(JwtClaimTypes.Role, "superadmin"),
    //            new Claim(JwtClaimTypes.Role, "admin")
    //        }
    //    }
    //})
    ;

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseIdentityServer();
app.UseAuthorization();
app.MapControllers();
app.Run();

3.Program里开始加东西(如果是历史的Net版本,是在StartUp里)

using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using MyIDP;
using MyIDP.Models;
using MyIDP.Permission;

var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
//由此重要
builder.Services.AddDbContext<IdpDbContext>(opt =>
{
    opt.UseMySql("server=127.0.0.1;Port=3306;database=AccountDb;uid=root;pwd=123456;", new MySqlServerVersion(new Version(8,0,29)));
});
builder.Services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddUserManager<MyUserManager>()
                .AddEntityFrameworkStores<IdpDbContext>()
                .AddDefaultTokenProviders();
builder.Services.AddIdentityServer()
    .AddDeveloperSigningCredential()
    .AddInMemoryIdentityResources(MyIDP.IdpConfig.GetIdentityResources())
    .AddInMemoryClients(MyIDP.IdpConfig.GetClients())
    .AddInMemoryApiScopes( MyIDP.IdpConfig.GetScope())
    .AddInMemoryApiResources( MyIDP.IdpConfig.GetApiResources())    //.AddResourceOwnerValidator<MyResourceOwnerPasswordValidator>() //这句可以打开自主验证登录用户
    //.AddProfileService<MyProfileService>()
    .AddAspNetIdentity<ApplicationUser>()
    //.AddTestUsers(new List<IdentityServer4.Test.TestUser>
    //{
    //    new IdentityServer4.Test.TestUser
    //    {
    //        SubjectId="123",
    //        Username = "alice",
    //        Password = "alice",
    //        Claims = new List<Claim>() {
    //            new Claim(JwtClaimTypes.Role, "superadmin"),
    //            new Claim(JwtClaimTypes.Role, "admin")
    //        }
    //    }
    //})
    ;
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}
app.UseIdentityServer();
app.UseAuthorization();
app.MapControllers();
app.Run();

因为使用的是内存储存t鉴权信息的方式,所以建立IdentityServer4的配置类IdpConfig

public static class IdpConfig
    {
        public static IEnumerable<IdentityResource> GetIdentityResources()
        {
            return new IdentityResource[]
            {
                new IdentityResources.OpenId(),
                new IdentityResources.Profile(),
                new IdentityResources.Address(),
                new IdentityResources.Phone(),
                new IdentityResources.Email()
            };
        }
        public static IEnumerable<ApiResource> GetApiResources()
        {
            //return new ApiResource[]
            //{
            //    new ApiResource("api1", "My API #1",new List<string>(){JwtClaimTypes.Role})
            //};
            //新写法
            return new[]
            {
                new ApiResource("api1", "My API #1")
                {
                    Scopes = { "scope1"}
                }
            };
        }
        public static IEnumerable<Client> GetClients()
        {
            return new[]
            {
                #region MyRegion
                 //// client credentials flow client
                //new Client
                //{
                //    ClientId = "console client",
                //    ClientName = "Client Credentials Client",

                //    AllowedGrantTypes = GrantTypes.ClientCredentials,

                //    ClientSecrets = { new Secret("511536EF-F270-4058-80CA-1C89C192F69A".Sha256()) },

                //    AllowedScopes = { "api1" }
                //},

    #endregion
                // wpf client, password grant
                new Client
                {
                    ClientId = "client",
                    AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
                    ClientSecrets =
                    {
                        new Secret("secret".Sha256())
                    },
                    AllowedScopes = //允许当访问的资源
                    {
                        "scope1",
                        //"api1",
                        IdentityServerConstants.StandardScopes.OpenId,
                        IdentityServerConstants.StandardScopes.Email,
                        IdentityServerConstants.StandardScopes.Address,
                        IdentityServerConstants.StandardScopes.Phone,
                        IdentityServerConstants.StandardScopes.Profile }
                }
            };
        }
        public static IEnumerable<ApiScope> GetScope()
        {
            return new ApiScope[] {
                new ApiScope("scope1"),
                new ApiScope("scope2"),
            };
        }
    }

数据库的usernamager

public class MyUserManager : UserManager<ApplicationUser>
    {
        public MyUserManager(IUserStore<ApplicationUser> store, IOptions<IdentityOptions> optionsAccessor, IPasswordHasher<ApplicationUser> passwordHasher,
          IEnumerable<IUserValidator<ApplicationUser>> userValidators, IEnumerable<IPasswordValidator<ApplicationUser>> passwordValidators, ILookupNormalizer keyNormalizer, IdentityErrorDescriber errors, IServiceProvider services, ILogger<UserManager<ApplicationUser>> logger)
           : base(store, optionsAccessor, new MyPasswordHasher(), userValidators, passwordValidators, keyNormalizer, errors, services, logger)
        {
            optionsAccessor.Value.Password.RequireDigit = false;
            optionsAccessor.Value.Password.RequiredLength = 4;
            optionsAccessor.Value.Password.RequireLowercase = false;
            optionsAccessor.Value.Password.RequireUppercase = false;
            optionsAccessor.Value.Password.RequireNonAlphanumeric = false;
        }

    }

重写验证密码的方法类MyResourceOwnerPasswordValidator,(如果没有打开Program中的AddResourceOwnerValidator<MyResourceOwnerPasswordValidator>() 则不需要)

public class MyResourceOwnerPasswordValidator : IResourceOwnerPasswordValidator
    {
        public readonly SignInManager<ApplicationUser> signInManager;
        private readonly MyUserManager userManager;
        //public readonly IEventService service;
        public MyResourceOwnerPasswordValidator(MyUserManager userService, SignInManager<ApplicationUser> signInManager)//, IEventService service)
        {
            userManager = userService;
            this.signInManager = signInManager;
            //this.service = service;
        }
        public async Task ValidateAsync(ResourceOwnerPasswordValidationContext context)
        {
            if (string.IsNullOrEmpty(context.UserName) || string.IsNullOrEmpty(context.Password))
            {
                context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant, "验证被拒绝,用户名或者密码为空。");
                return;
            }
            var user = await userManager.FindByNameAsync(context.UserName);
            if (user == null)
            {
                context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant, "验证失败,不存在当前用户。");
                return;
            }
            //检验用户密码(虽然我也不知道他的密码是采用什么加密方式得到的,但是我也不需要知道) 
            var passwordPass = await userManager.CheckPasswordAsync(user, context.Password);
            if (!passwordPass)
            {
                context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant, "验证失败,用户凭证错误");
                return;
            }
            else
            {
                try
                {
                    await userManager.AddLoginAsync(user, new UserLoginInfo(user.Id, "", user.UserName));
                }
                catch (Exception ex)
                {
                    ;
                }
                finally
                {
                    context.Result = new GrantValidationResult(user.Id, GrantType.ResourceOwnerPassword, new List<Claim>() { new Claim("account", user.UserName) }); 
                }
            }
            return;
        }
    }

MyPasswordHasher

public class MyPasswordHasher : PasswordHasher<ApplicationUser>
    {
        public override string HashPassword(ApplicationUser user, string password)
        {
            //PasswordHasher<ApplicationUser> ph = new PasswordHasher<ApplicationUser>();
            //var pstr = ph.HashPassword(new ApplicationUser(), password);
            //return pstr;
            return password.MD5();
        }

        public override PasswordVerificationResult VerifyHashedPassword(ApplicationUser user, string hashedPassword, string providedPassword)
        {
            if (providedPassword.MD5().Equals(hashedPassword))
            {
                return PasswordVerificationResult.Success;
            }
            else
            {
                return PasswordVerificationResult.Failed;
            }
        }
    }

创建自己的User类 ApplicationUser继承 IdentityUser  复写自带的AspNetUser表

public class ApplicationUser : IdentityUser
    {
        public string MySomething { get; set; } = "";
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }
        /// <summary>
        /// 创建人Id
        /// </summary>
        public string CreatorId { get; set; } = "";
        /// <summary>
        /// 否已删除
        /// </summary>
        public bool Deleted { get; set; }
        /// <summary>
        /// 姓名
        /// </summary>
        public string RealName { get; set; }
        /// <summary>
        /// 性别
        /// </summary>
        public Sex Sex { get; set; }
        /// <summary>
        /// 出生日期
        /// </summary>
        public DateTime? Birthday { get; set; }
        /// <summary>
        /// 所属部门Id
        /// </summary>
        public string DepartmentId { get; set; } = "";
        public string OtherData { get; set; } = "";
        // 用户角色 用户权限 用户信息 用户登录tokens  重新绑定与父类的关系 命名必须和父类一致
        public virtual ICollection<IdentityUserRole<string>> UserRoles { get; set; }
        public virtual ICollection<IdentityUserClaim<string>> Claims { get; set; }
        public virtual ICollection<IdentityUserLogin<string>> Logins { get; set; }
        public virtual ICollection<IdentityUserToken<string>> Tokens { get; set; }
    }
    public enum Sex
    {
        [Description("男")]
        Man = 1,
        [Description("女")]
        Woman = 0
    }

至此可以生成数据库迁移后 Postman测试一下:

到此这篇关于.Net6集成IdentityServer4 +AspNetCore Identity读取数据表用户且鉴权授权管理API的文章就介绍到这了,更多相关.Net6 读取数据表用户内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • .net 运用二进制位运算进行数据库权限管理

    .net 运用二进制位运算进行数据库权限管理

    .net 运用二进制位运算进行数据库权限管理 ,需要的朋友可以参考一下
    2013-02-02
  • Javascript调用Webservice的多种方法

    Javascript调用Webservice的多种方法

    通过xmlhttp+webservice(原始方法)
    2009-02-02
  • .net通过Action进行Options参数的传递的方法

    .net通过Action进行Options参数的传递的方法

    在.NET Core中,使用Action和Options参数方式配置服务并将配置信息对象注册到IServiceCollection的好处在于,它提供了更高级别的可配置性和可扩展性,这篇文章主要介绍了.net通过Action进行Options参数的传递,你知道是怎么实现的吗,需要的朋友可以参考下
    2023-12-12
  • asp.net上传文件到数据库的解决方案

    asp.net上传文件到数据库的解决方案

    这篇文章主要介绍了ASP.NET上传文件到数据库,先从文字上了解一下上传文件到数据库的具体步骤,再从代码上来实现,需要的朋友可以参考下
    2015-09-09
  • 用.NET如何生成二维码

    用.NET如何生成二维码

    二维码是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的,下面介绍一下如何用.NET生成二维码(QR Code码制),需要的朋友可以参考下
    2015-10-10
  • Asp.Mvc 2.0实现用户登录与注销功能实例讲解(2)

    Asp.Mvc 2.0实现用户登录与注销功能实例讲解(2)

    这篇文章主要介绍了Asp.Mvc 2.0实现用户登录与注销功能,用户登录方式都是FORM表单验证方式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2015-08-08
  • ASP.NET中实现jQuery Validation-Engine的Ajax验证实现代码

    ASP.NET中实现jQuery Validation-Engine的Ajax验证实现代码

    在jQuery的表变验证插件中Validation-Engine是一款高质量的产品,提示效果非常精美,而且里面包含了AJAX验证功能
    2012-05-05
  • Entity Framework管理一对一实体关系

    Entity Framework管理一对一实体关系

    本文详细讲解了Entity Framework管理一对一实体关系的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-03-03
  • C# Lambda表达式用途深入理解

    C# Lambda表达式用途深入理解

    假如我们想要从一个整型数组中取出其中是奇数的选项,其实现方式有很多,接下来通过三种方法的对比理解Lambda表达式的用途,需要了解的朋友可以参考下
    2012-12-12
  • .NET数组使用中的注意事项小结

    .NET数组使用中的注意事项小结

    这篇文章主要介绍了.NET数组使用中的注意事项,总结了常见的三个数组使用中的注意事项,对于.NET初学者有一定的参考借鉴价值,需要的朋友可以参考下
    2014-12-12

最新评论