Exemplo n.º 1
0
        public static string IssueJwt(TokenModelJwt tokenModel)
        {
            string issue  = AppSettingHelper.App("JWT", "Issue");  // "Issuer";
            string aud    = AppSettingHelper.App("JWT", "Aud");    // "Audience";
            string secret = AppSettingHelper.App("JWT", "Secret"); // "ghgfopkhop gkfdopg kdfpgkdfg dfgkdfg dfgf gfdg";


            //var jwt1 = ;

            //var claims = new List<Claim>
            //{
            //    new Claim("jti", tokenModel.Uid.ToString()),
            //    new Claim("iat", $"{new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds()}"),
            //    new Claim("nbf",$"{new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds()}") ,
            //    //这个就是过期时间,目前是过期1000秒,可自定义,注意JWT有自己的缓冲过期时间
            //    new Claim ("exp",$"{new DateTimeOffset(DateTime.Now.AddSeconds(1000)).ToUnixTimeSeconds()}"),
            //    new Claim("iss",issue ),
            //    new Claim("aud",aud),
            //};

            var claims = new List <Claim>
            {
                //下边为Claim的默认配置
                new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
                new Claim(JwtRegisteredClaimNames.Iat, $"{new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds()}"),
                new Claim(JwtRegisteredClaimNames.Nbf, $"{new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds()}"),
                //这个就是过期时间,目前是过期100秒,可自定义,注意JWT有自己的缓冲过期时间
                new Claim(JwtRegisteredClaimNames.Exp, $"{new DateTimeOffset(DateTime.Now.AddSeconds(10000)).ToUnixTimeSeconds()}"),
                new Claim(JwtRegisteredClaimNames.Iss, issue),
                new Claim(JwtRegisteredClaimNames.Aud, aud),
                //这个Role是官方UseAuthentication要要验证的Role,我们就不用手动设置Role这个属性了
                //new Claim(ClaimTypes.Role,tokenModel.Role),
                new Claim(ClaimTypes.Name, tokenModel.Name),
                new Claim(ClaimTypes.NameIdentifier, tokenModel.Id.ToString()),
            };


            claims.AddRange(tokenModel.Role.Select(s => new Claim(ClaimTypes.Role, s.Trim())));

            //秘钥 (SymmetricSecurityKey 对安全性的要求,密钥的长度太短会报出异常)
            var key   = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secret));
            var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

            var jwt = new JwtSecurityToken
                      (
                issuer: issue,
                claims: claims,
                signingCredentials: creds

                      );

            var jwtHandler = new JwtSecurityTokenHandler();
            var encodedJwt = jwtHandler.WriteToken(jwt);

            return(encodedJwt);
        }
Exemplo n.º 2
0
        public static void UseSeedDataMildd(this IApplicationBuilder app, Dbcontext dbcontext)
        {
            if (AppSettingHelper.App("SeedDBEnabled:table").ObjToBool())
            {
                dbcontext.Db.DbMaintenance.CreateDatabase();
                dbcontext.Db.CodeFirst.InitTables(typeof(BlogArticle));
                dbcontext.Db.CodeFirst.InitTables(typeof(BlogTag));
                dbcontext.Db.CodeFirst.InitTables(typeof(Category));
                dbcontext.Db.CodeFirst.InitTables(typeof(Tag));
                dbcontext.Db.CodeFirst.InitTables(typeof(User));
                dbcontext.Db.CodeFirst.InitTables(typeof(Role));
                dbcontext.Db.CodeFirst.InitTables(typeof(UserRole));
                dbcontext.Db.CodeFirst.InitTables(typeof(Menu));
                dbcontext.Db.CodeFirst.InitTables(typeof(MenuPermission));
                dbcontext.Db.CodeFirst.InitTables(typeof(Button));
                dbcontext.Db.CodeFirst.InitTables(typeof(ButtonPermission));
                dbcontext.Db.CodeFirst.InitTables(typeof(ApiMethod));
                dbcontext.Db.CodeFirst.InitTables(typeof(ApiMethodPermission));
            }

            if (AppSettingHelper.App("SeedDBEnabled:data").ObjToBool())
            {
                if (!dbcontext.GetSimpleClient <Category>().GetList().Any())
                {
                    dbcontext.GetSimpleClient <Category>().Insert(new Category
                    {
                        Name   = "笑话",
                        SeqNum = 1,

                        Floor    = 1,
                        ParentId = 0
                    });
                }

                if (!dbcontext.GetSimpleClient <Role>().GetList().Any())
                {
                    dbcontext.GetSimpleClient <Role>().Insert(new Role
                    {
                        Code = "superAdmin",
                        Name = "超级管理员"
                    });

                    dbcontext.GetSimpleClient <Role>().Insert(new Role
                    {
                        Code = "admin",
                        Name = "管理员"
                    });


                    dbcontext.GetSimpleClient <Role>().Insert(new Role
                    {
                        Code = "nomal",
                        Name = "普通用户"
                    });


                    dbcontext.GetSimpleClient <User>().Insert(new User
                    {
                        Account   = "su",
                        Password  = "******",
                        LoginTime = DateTime.Now,
                        NickName  = "su11"
                    });

                    dbcontext.GetSimpleClient <User>().Insert(new User
                    {
                        Account   = "ad",
                        Password  = "******",
                        LoginTime = DateTime.Now,
                        NickName  = "ad11"
                    });

                    dbcontext.GetSimpleClient <UserRole>().Insert(new UserRole
                    {
                        RoleId = 1,
                        UserId = 1
                    });

                    dbcontext.GetSimpleClient <UserRole>().Insert(new UserRole
                    {
                        RoleId = 2,
                        UserId = 2
                    });
                }

                if (!dbcontext.GetSimpleClient <Menu>().GetList().Any())
                {
                    dbcontext.GetSimpleClient <Menu>().InsertRange(new Menu[] {
                        new Menu
                        {
                            Id       = 1,
                            Code     = "home",
                            Name     = "XJJXMM欢迎页",
                            Icon     = "icon-home",
                            ParentId = 0,
                            Route    = "",
                            SeqNum   = 1
                        },
                        new Menu
                        {
                            Id       = 2,
                            Code     = "us",
                            Name     = "用户管理",
                            Icon     = "icon-home",
                            ParentId = 0,
                            Route    = "",
                            SeqNum   = 1
                        },
                        new Menu
                        {
                            Id       = 3,
                            Code     = "user",
                            Name     = "用户管理",
                            Icon     = "icon-home",
                            ParentId = 2,
                            Route    = "/user",
                            SeqNum   = 1
                        },
                        new Menu
                        {
                            Id       = 4,
                            Code     = "role",
                            Name     = "角色管理",
                            Icon     = "icon-home",
                            ParentId = 2,
                            Route    = "/role",
                            SeqNum   = 1
                        },
                        new Menu
                        {
                            Id       = 5,
                            Code     = "mu",
                            Name     = "菜单管理",
                            Icon     = "icon-home",
                            ParentId = 0,
                            Route    = "",
                            SeqNum   = 1
                        },
                        new Menu
                        {
                            Id       = 6,
                            Code     = "menu",
                            Name     = "菜单管理",
                            Icon     = "icon-home",
                            ParentId = 5,
                            Route    = "/menu",
                            SeqNum   = 1
                        },
                        new Menu
                        {
                            Id       = 7,
                            Code     = "perm",
                            Name     = "权限管理",
                            Icon     = "icon-home",
                            ParentId = 5,
                            Route    = "/perm",
                            SeqNum   = 1
                        }
                    });
                }

                if (!dbcontext.GetSimpleClient <MenuPermission>().GetList().Any())
                {
                    dbcontext.GetSimpleClient <MenuPermission>().InsertRange(new MenuPermission[] {
                        new MenuPermission
                        {
                            Id     = 1,
                            MenuId = 1,
                            RoleId = 1
                        },
                        new MenuPermission
                        {
                            Id     = 2,
                            MenuId = 2,
                            RoleId = 1
                        },
                        new MenuPermission
                        {
                            Id     = 3,
                            MenuId = 3,
                            RoleId = 1
                        },
                    });
                }
                //db.GetSimpleClient<Category>().
            }
        }