From c872d7ae0951726a6f757e58895a00e62d4312a9 Mon Sep 17 00:00:00 2001 From: Author Name Date: Thu, 24 Jun 2021 22:49:56 +0800 Subject: [PATCH 01/33] RESTful --- ...I\347\254\254\344\272\214\350\212\202).md" | 138 ++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 "\346\235\250\345\256\207\346\226\214/note-2021-06-23(web API\347\254\254\344\272\214\350\212\202).md" diff --git "a/\346\235\250\345\256\207\346\226\214/note-2021-06-23(web API\347\254\254\344\272\214\350\212\202).md" "b/\346\235\250\345\256\207\346\226\214/note-2021-06-23(web API\347\254\254\344\272\214\350\212\202).md" new file mode 100644 index 0000000..7606d3f --- /dev/null +++ "b/\346\235\250\345\256\207\346\226\214/note-2021-06-23(web API\347\254\254\344\272\214\350\212\202).md" @@ -0,0 +1,138 @@ +# 2021-6-23 web API 第二节 + ++ dotnet build 检查 + ++ Prettier - Code formatter 格式化插件名 ++ RESTClient 一款自动化测试REST API的工具,它可以自动化测试RESTful API并生成精美的测试报告,同时基于测试过的历史API,可以生成精美的RESTful API文档。 + ++ dynamic的出现让C#具有了弱语言类型的特性。编译器在编译的时候不再对类型进行检查,编译期默认dynamic对象支持你想要的任何特性。比如,即使你对GetDynamicObject方法返回的对象一无所知,编译器也不会报错 + +1. 在项目内创建Entity文件夹 +``` +User.cs +using System; + +namespace myApi.Api.Entity +{ + public class User + { + public User(){ + createTime=DateTime.Now; + UpdateTime=DateTime.Now; + } + public int Id { get; set; } + + public string Username { get; set; } + + public string Password { get; set; } + + public DateTime createTime { get; set; } + + public DateTime UpdateTime { get; set; } + + public string Remark { get; set; } + } +} +``` +2. 在项目下Controllers 下创建 UserConteroller.cs +``` +using System.Collections.Generic; +using System.Linq; +using Microsoft.AspNetCore.Mvc; +using myApi.Api.Entity; + +namespace myApi.Api.Controllers +{ + [ApiController] + [Route("[controller]")] + public class UserController : ControllerBase + { + [HttpGet] + public IEnumerable Get() + { + var user = GetUsers(); + return user; + } + + [HttpGet("{id}")] + public dynamic Get(int id) + { + var users = GetUsers(); + var user = users.Where(x => x.Id == id).FirstOrDefault(); + return user; + } + + [HttpPost] + public dynamic Post(dynamic model) + { + return new{ + Code=1000, + Data=model, + Msg="创建成功" + }; + } + + [HttpPut("{id}")] + public dynamic Put(int id,dynamic model) + { + return new{ + Code=1000, + Data=model, + Msg=string.Format("你修改的id为:{0},修改成功",id) + }; + } + + [HttpDelete("{id}")] + public dynamic Delete(int id) + { + return new{ + Code=1000, + Msg=string.Format("你删除的id为:{0},删除成功",id) + }; + } + + + + private IEnumerable GetUsers() + { + var user = + new List { + new User { Id = 1, Username = "root", Password = "123" }, + new User { Id = 2, Username = "admin", Password = "123" } + }; + return user; + } + } +} +``` +3. RESTClient +``` +api.http +### 获取用户 + +GET http://localhost:5000/User HTTP/1.1 + +###获取指定id用户 +GET http://localhost:5000/User/1 HTTP/1.1 + +###创建用户 +POST http://localhost:5000/User HTTP/1.1 +Content-Type:application/json + +{ + "username":"123", + "password":"1113" +} + +###修改指定用户 +PUT http://localhost:5000/User/1 HTTP/1.1 +Content-Type: application/json + +{ + "username":"123", + "password":"1113" +} + +###删除指定用户 +DELETE http://localhost:5000/User/1 HTTP/1.1 +``` -- Gitee From 5db93ca0a12d5b5fdd7516cac5af73db86de980a Mon Sep 17 00:00:00 2001 From: Author Name Date: Mon, 28 Jun 2021 13:02:27 +0800 Subject: [PATCH 02/33] =?UTF-8?q?sqlserver=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\344\270\211\350\212\202\350\257\276).md" | 155 ++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100644 "\346\235\250\345\256\207\346\226\214/note-2021-06-26(web APl\347\254\254\344\270\211\350\212\202\350\257\276).md" diff --git "a/\346\235\250\345\256\207\346\226\214/note-2021-06-26(web APl\347\254\254\344\270\211\350\212\202\350\257\276).md" "b/\346\235\250\345\256\207\346\226\214/note-2021-06-26(web APl\347\254\254\344\270\211\350\212\202\350\257\276).md" new file mode 100644 index 0000000..0b904e5 --- /dev/null +++ "b/\346\235\250\345\256\207\346\226\214/note-2021-06-26(web APl\347\254\254\344\270\211\350\212\202\350\257\276).md" @@ -0,0 +1,155 @@ +# 2021-6-26 web API 第三节 + +## ef Core +1. 安装 Entity Framework Core +``` +dotnet add package Microsoft.EntityFrameworkCore.Sqlserver +//数据库为sqlserver +``` +2. 在项目文件夹中,创建Entity文件夹,添加User.cs,Student.cs等 定义字段 +3. 在项目文件夹中,创建Data文件夹,添加myApiDb.cs +``` +using MyApi.Api.Entity; +using Microsoft.EntityFrameworkCore; +namespace MyApi.Api.Db +{ + public class myApiDb: DbContext + { + public DbSet User {get;set;} + public DbSet Student {get;set;} + + protected override void OnConfiguring(DbContextOptionsBuilder options) + => options.UseSqlServer(@"server=.;uid=sa;pwd=123456;database=Admin1000"); + } +} +``` +4. 创建数据库 使用迁移创建数据库 +``` +dotnet tool install --global dotnet-ef +dotnet add package Microsoft.EntityFrameworkCore.Design +dotnet ef migrations add InitialCreate +dotnet ef database update +``` +5. 在项目文件夹中,创建ParamModel文件夹,添加newUser.cs 模型 +``` +namespace MyApi.Api.ParamModel +{ + public class newUser{ + public string Username{get;set;} + public string Password{get;set;} + } +} +``` +6. 在项目下Controllers 的 UserConteroller.cs +``` +using System.Collections.Generic; +using System.Linq; +using Microsoft.AspNetCore.Mvc; +using MyApi.Api.Entity; +using MyApi.Api.ParamModel; +using System; + +namespace MyApi.Api.Controllers +{ + [ApiController] + [Route("[controller]")] + public class UserController : ControllerBase + { + private myApi.Api.Db.myApiDb db = new Api.Db.myApiDb(); + + + [HttpGet] + public IEnumerable Get() + { + var user = db.User.ToList(); + return user; + } + + [HttpGet("{id}")] + public dynamic Get(int id) + { + var user = db.User.Where(x => x.Id == id).FirstOrDefault(); + return user; + } + + [HttpPost] + public dynamic post(newUser model) + { + var user=new User{ + Username=model.Username, + Password=model.Password, + }; + db.User.Add(user); + db.SaveChanges();//保存 + + return new + { + Code=1000, + Data =user, + ParallelMergeOptions="创建成功" + }; + } + + [HttpPut("{id}")] + public dynamic Put(int id,newUser model) + { + var user = db.User.Where(x => x.Id == id).FirstOrDefault(); + + if (user != null) + { + user.Username = model.Username; + user.Password = model.Password; + user.UpdateTime=DateTime.Now; + db.Update(user); + db.SaveChanges(); + return new + { + Code = 1000, + Data = user, + Msg = string.Format("你修改的用户的id为:{0},已经修改成功,请注意查收", id) + }; + } + else + { + return new + { + Code = 104, + Data = "", + Msg = "指定Id的用户不存在,请确认后重试" + }; + } + + + } + + [HttpDelete("{id}")] + public dynamic Delete(int id) + { + var user = db.User.Where(x => x.Id == id).FirstOrDefault(); + if (user != null) + { + db.User.Remove(user); + db.SaveChanges(); + return new + { + Code = 1000, + Msg = string.Format("删除指定id为{0}的用户成功", id) + }; + } + else + { + return new + { + Code = 104, + Data = "", + Msg = "指定Id的用户不存在,请确认后重试" + }; + } + } + + + } +} + +``` +7. RESTClient 测试 -- Gitee From 50314547e2c089c6a293ca4aed779ec8eb31b074 Mon Sep 17 00:00:00 2001 From: Author Name Date: Wed, 30 Jun 2021 11:28:11 +0800 Subject: [PATCH 03/33] =?UTF-8?q?=E5=BB=BA=E7=AB=8B=E8=A1=A8=E4=B9=8B?= =?UTF-8?q?=E9=97=B4=E7=9A=84=E5=85=B3=E7=B3=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...I\347\254\254\345\233\233\350\212\202).md" | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 "\346\235\250\345\256\207\346\226\214/note-2021-06-29(web API\347\254\254\345\233\233\350\212\202).md" diff --git "a/\346\235\250\345\256\207\346\226\214/note-2021-06-29(web API\347\254\254\345\233\233\350\212\202).md" "b/\346\235\250\345\256\207\346\226\214/note-2021-06-29(web API\347\254\254\345\233\233\350\212\202).md" new file mode 100644 index 0000000..6ef9045 --- /dev/null +++ "b/\346\235\250\345\256\207\346\226\214/note-2021-06-29(web API\347\254\254\345\233\233\350\212\202).md" @@ -0,0 +1,52 @@ +# 2021-6-29 web API 第四节 建立表之间的关系 +1. 包装一下Entity下的.cs的公共字段 创建BaseEntity.cs +``` +using System; +namespace MyApi.Api.Entity +{ + public abstract class BaseEntity{ + public BaseEntity(){ + CreatedTime=DateTime.Now; + UpdatedTime=DateTime.Now; + } + public int Id{get;set;} + public bool IsActived{get;set;} + public bool IsDeleted{get;set;} + public DateTime CreatedTime{get;set;} + public DateTime UpdatedTime{get;set;} + public string Remarks{get;set;} + } +} + +如:在Users.cs定义字段下继承 public class Users : BaseEntity +``` +2. 建立表之间的关系 Entity文件夹下 +``` +Users.cs + public class Users : BaseEntity + { + public string Username { get; set; } + + public string Password { get; set; } + + public virtual IEnumerable UserRoles { get; set; } + + } + +UserRoles.cs + public class UserRoles : BaseEntity + { + public int UserId { get; set; } + public int RoleId { get; set; } + public virtual Users User { get; set; } + public virtual Roles Role { get; set; } + } + +Roles.cs + public class Roles:BaseEntity + { + public string RoleName { get; set; } + public virtual IEnumerable UserRoles { get; set; } + } +``` +### C#泛型集合 using System.Collections.Generic \ No newline at end of file -- Gitee From 4526b18b8edc5a568c8aef752564c0f615f6d00f Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 30 Jun 2021 17:19:37 +0800 Subject: [PATCH 04/33] =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...54\254\344\272\224\350\212\202 \346\216\245\345\217\243).md" | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 "\346\235\250\345\256\207\346\226\214/note-2021-06-30(\347\254\254\344\272\224\350\212\202 \346\216\245\345\217\243).md" diff --git "a/\346\235\250\345\256\207\346\226\214/note-2021-06-30(\347\254\254\344\272\224\350\212\202 \346\216\245\345\217\243).md" "b/\346\235\250\345\256\207\346\226\214/note-2021-06-30(\347\254\254\344\272\224\350\212\202 \346\216\245\345\217\243).md" new file mode 100644 index 0000000..dee1f13 --- /dev/null +++ "b/\346\235\250\345\256\207\346\226\214/note-2021-06-30(\347\254\254\344\272\224\350\212\202 \346\216\245\345\217\243).md" @@ -0,0 +1,2 @@ +# 2021-6-29 web API 第五节 接口 ++ IQueryable 是将Skip ,take 这些方法表达式翻译成T-SQL语句之后再向SQL服务器发送命令,它并不是把所有数据都加载到内存里来才进行条件过滤。 -- Gitee From 7daea389423a6802d401fdc77cb17b96a51a67d3 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 2 Jul 2021 10:50:18 +0800 Subject: [PATCH 05/33] =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\350\212\202 \346\216\245\345\217\243).md" | 78 +++++++++++++++++++ ...\350\212\202 \346\216\245\345\217\243).md" | 2 - ...5\350\265\226\346\263\250\345\205\245).md" | 0 3 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 "\346\235\250\345\256\207\346\226\214/note-2021-06-30(web API \347\254\254\344\272\224\350\212\202 \346\216\245\345\217\243).md" delete mode 100644 "\346\235\250\345\256\207\346\226\214/note-2021-06-30(\347\254\254\344\272\224\350\212\202 \346\216\245\345\217\243).md" create mode 100644 "\346\235\250\345\256\207\346\226\214/note-2021-07-02(web API\347\254\254\345\205\255\350\212\202 \344\275\277\347\224\250\344\276\235\350\265\226\346\263\250\345\205\245).md" diff --git "a/\346\235\250\345\256\207\346\226\214/note-2021-06-30(web API \347\254\254\344\272\224\350\212\202 \346\216\245\345\217\243).md" "b/\346\235\250\345\256\207\346\226\214/note-2021-06-30(web API \347\254\254\344\272\224\350\212\202 \346\216\245\345\217\243).md" new file mode 100644 index 0000000..20c4d35 --- /dev/null +++ "b/\346\235\250\345\256\207\346\226\214/note-2021-06-30(web API \347\254\254\344\272\224\350\212\202 \346\216\245\345\217\243).md" @@ -0,0 +1,78 @@ +# 2021-6-29 web API 第五节 接口 ++ IQueryable 是将Skip ,take 这些方法表达式翻译成T-SQL语句之后再向SQL服务器发送命令,它并不是把所有数据都加载到内存里来才进行条件过滤。 ++ interface 接口 +``` +异步多线程Task(任务) +Task InsertAsync(T entity); +Task UpdateAsync(T entity); +``` ++ IRepository.cs +``` +using System.Linq; +using System.Threading.Tasks; +namespace MyApi.Api.Repository +{ + //interface 接口 + public interface IRepository + { + //IQueryable 是将Skip ,take 这些方法表达式翻译成T-SQL语句之后再向SQL服务器发送命令,它并不是把所有数据都加载到内存里来才进行条件过滤。 + IQueryable Table { get; } + + //根据Id获取指定实体 + T GetById(int id); + + // 使用实体对象,插入数据 + void Insert(T entity); + + // 使用实体对象,插入数据(异步) + Task InsertAsync(T entity); + + //使用实体对象,更新数据 + void Update(T entity); + + //使用实体对象,更新数据(异步) + Task UpdateAsync(T entity); + + //使用实体对象,删除数据 + void Delete(int id); + } +} +``` + ++ EfRepository.cs +``` + public class EfRepository : IRepository where T : BaseEntity + { + // 数据库上下文的变量,此处是使用常规手段,直接初始化一个数据库上下文的实例对象 + + private Admin3000Db _db; + + public EfRepository(Admin3000Db db) + { + _db=db; + } + //一个字段成员,用于内部Entity属性 + private DbSet _entity; + + // 一个访问受限的属性,只有访问器,总是返回一个代表T类型的表对象 + protected DbSet Entity + { + get + { + // 如果 _entity为空(没有指向一个对象),则从数据库上下文重新获得一个 + if (_entity == null) + { + _entity = _db.Set(); + } + return _entity; + } + } + //代表一个可以用于查询T类型的表 + public IQueryable Table + { + get + { + return Entity.AsQueryable(); + } + } +``` \ No newline at end of file diff --git "a/\346\235\250\345\256\207\346\226\214/note-2021-06-30(\347\254\254\344\272\224\350\212\202 \346\216\245\345\217\243).md" "b/\346\235\250\345\256\207\346\226\214/note-2021-06-30(\347\254\254\344\272\224\350\212\202 \346\216\245\345\217\243).md" deleted file mode 100644 index dee1f13..0000000 --- "a/\346\235\250\345\256\207\346\226\214/note-2021-06-30(\347\254\254\344\272\224\350\212\202 \346\216\245\345\217\243).md" +++ /dev/null @@ -1,2 +0,0 @@ -# 2021-6-29 web API 第五节 接口 -+ IQueryable 是将Skip ,take 这些方法表达式翻译成T-SQL语句之后再向SQL服务器发送命令,它并不是把所有数据都加载到内存里来才进行条件过滤。 diff --git "a/\346\235\250\345\256\207\346\226\214/note-2021-07-02(web API\347\254\254\345\205\255\350\212\202 \344\275\277\347\224\250\344\276\235\350\265\226\346\263\250\345\205\245).md" "b/\346\235\250\345\256\207\346\226\214/note-2021-07-02(web API\347\254\254\345\205\255\350\212\202 \344\275\277\347\224\250\344\276\235\350\265\226\346\263\250\345\205\245).md" new file mode 100644 index 0000000..e69de29 -- Gitee From 1b263d58d8511935953ef205b6f278e979828f7b Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 2 Jul 2021 11:03:31 +0800 Subject: [PATCH 06/33] =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\224\250\344\276\235\350\265\226\346\263\250\345\205\245).md" | 1 + 1 file changed, 1 insertion(+) diff --git "a/\346\235\250\345\256\207\346\226\214/note-2021-07-02(web API\347\254\254\345\205\255\350\212\202 \344\275\277\347\224\250\344\276\235\350\265\226\346\263\250\345\205\245).md" "b/\346\235\250\345\256\207\346\226\214/note-2021-07-02(web API\347\254\254\345\205\255\350\212\202 \344\275\277\347\224\250\344\276\235\350\265\226\346\263\250\345\205\245).md" index e69de29..23a25ce 100644 --- "a/\346\235\250\345\256\207\346\226\214/note-2021-07-02(web API\347\254\254\345\205\255\350\212\202 \344\275\277\347\224\250\344\276\235\350\265\226\346\263\250\345\205\245).md" +++ "b/\346\235\250\345\256\207\346\226\214/note-2021-07-02(web API\347\254\254\345\205\255\350\212\202 \344\275\277\347\224\250\344\276\235\350\265\226\346\263\250\345\205\245).md" @@ -0,0 +1 @@ +# 2021-7-2 web API web API第六节 使用依赖注入 -- Gitee From 992b546065ff11f8ce7549f60f0cc845c62094de Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 2 Jul 2021 11:17:21 +0800 Subject: [PATCH 07/33] =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...24\250\344\276\235\350\265\226\346\263\250\345\205\245).md" | 3 +++ 1 file changed, 3 insertions(+) diff --git "a/\346\235\250\345\256\207\346\226\214/note-2021-07-02(web API\347\254\254\345\205\255\350\212\202 \344\275\277\347\224\250\344\276\235\350\265\226\346\263\250\345\205\245).md" "b/\346\235\250\345\256\207\346\226\214/note-2021-07-02(web API\347\254\254\345\205\255\350\212\202 \344\275\277\347\224\250\344\276\235\350\265\226\346\263\250\345\205\245).md" index 23a25ce..54951eb 100644 --- "a/\346\235\250\345\256\207\346\226\214/note-2021-07-02(web API\347\254\254\345\205\255\350\212\202 \344\275\277\347\224\250\344\276\235\350\265\226\346\263\250\345\205\245).md" +++ "b/\346\235\250\345\256\207\346\226\214/note-2021-07-02(web API\347\254\254\345\205\255\350\212\202 \344\275\277\347\224\250\344\276\235\350\265\226\346\263\250\345\205\245).md" @@ -1 +1,4 @@ # 2021-7-2 web API web API第六节 使用依赖注入 + ++ dotnet restore - 恢复项目的依赖项和工具。 + -- Gitee From 6cead6f8ab18bbbb5a85fe6d7852a3276184b0a4 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 3 Jul 2021 17:34:44 +0800 Subject: [PATCH 08/33] =?UTF-8?q?C#=E4=B8=89=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\345\236\213 \345\261\236\346\200\247).md" | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 "\346\235\250\345\256\207\346\226\214/note-2021-07-03(C#\345\237\272\347\241\200\344\270\211\347\202\271\346\216\245\345\217\243 \346\263\233\345\236\213 \345\261\236\346\200\247).md" diff --git "a/\346\235\250\345\256\207\346\226\214/note-2021-07-03(C#\345\237\272\347\241\200\344\270\211\347\202\271\346\216\245\345\217\243 \346\263\233\345\236\213 \345\261\236\346\200\247).md" "b/\346\235\250\345\256\207\346\226\214/note-2021-07-03(C#\345\237\272\347\241\200\344\270\211\347\202\271\346\216\245\345\217\243 \346\263\233\345\236\213 \345\261\236\346\200\247).md" new file mode 100644 index 0000000..c3c32e9 --- /dev/null +++ "b/\346\235\250\345\256\207\346\226\214/note-2021-07-03(C#\345\237\272\347\241\200\344\270\211\347\202\271\346\216\245\345\217\243 \346\263\233\345\236\213 \345\261\236\346\200\247).md" @@ -0,0 +1,62 @@ +# C# 接口 泛型 属性 + ++ dotnet new console 创建C#控制台 ++ 一般用于函数的返回值。如果函数不需要返回值,即可以用void做返回类型 + +1. 属性 +``` +namespace Test.Console +{ + //构造函数 + public Pig(){ + _age=10; + } + public int _age; + public int Age + { + //get是读出属性获取的值 set是修改属性的值 + get + { + return _age; + } + set + { + _age = value; + } + } + + public void SetAge(int age){ + _age=age; + } + public int GetAge(){ + return _age; + } +} +``` ++ 接口 +``` +namespace Test.Console +{ + public interface IZoo + { + int Age { get; set; } + void Eat(); + void Sleep(); + } + public class Tiger : IZoo + { + public int Age { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } + + public void Eat() + { + throw new NotImplementedException(); + } + + public void Sleep() + { + throw new NotImplementedException(); + } + } + +} +``` \ No newline at end of file -- Gitee From d9ebaced19e430e97d4fe45c26cab6fe2c94349d Mon Sep 17 00:00:00 2001 From: Author Name Date: Mon, 5 Jul 2021 21:05:19 +0800 Subject: [PATCH 09/33] =?UTF-8?q?C#=E4=B8=89=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\345\236\213 \345\261\236\346\200\247).md" | 46 ++++++++++++++++++- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git "a/\346\235\250\345\256\207\346\226\214/note-2021-07-03(C#\345\237\272\347\241\200\344\270\211\347\202\271\346\216\245\345\217\243 \346\263\233\345\236\213 \345\261\236\346\200\247).md" "b/\346\235\250\345\256\207\346\226\214/note-2021-07-03(C#\345\237\272\347\241\200\344\270\211\347\202\271\346\216\245\345\217\243 \346\263\233\345\236\213 \345\261\236\346\200\247).md" index c3c32e9..81de4f2 100644 --- "a/\346\235\250\345\256\207\346\226\214/note-2021-07-03(C#\345\237\272\347\241\200\344\270\211\347\202\271\346\216\245\345\217\243 \346\263\233\345\236\213 \345\261\236\346\200\247).md" +++ "b/\346\235\250\345\256\207\346\226\214/note-2021-07-03(C#\345\237\272\347\241\200\344\270\211\347\202\271\346\216\245\345\217\243 \346\263\233\345\236\213 \345\261\236\346\200\247).md" @@ -33,7 +33,7 @@ namespace Test.Console } } ``` -+ 接口 +2. 接口 ``` namespace Test.Console { @@ -59,4 +59,46 @@ namespace Test.Console } } -``` \ No newline at end of file +``` +接口的使用 +``` +namespace Test.Console +{ + class Program + { + static void Main(string[] args) + { + IZoo eat1=new Tiger(); + IZoo eat2=new Dog(); + eat1.Eat(); + eat2.Eat(); + } + } +} +``` +3. 泛型 +拿笔来说, Pen 上没有标签就一只 Pen ,不知道它什么颜色的, +Pen 留了一个标签空位,写Red就成了 Pen,你就知道是写出红色的笔了。 +``` +namespace Test.Console +{ + public interface IPen{ + void Class(T Pen); + } + + public class RedPen : IPen + { + public void Class(T Pen) + { + System.Console.WriteLine("这是红色的笔"); + } + } + public class WhitePen : IPen + { + public void Class(T Pen) + { + System.Console.WriteLine("这是白色的笔"); + } + } +} +``` -- Gitee From 28d602ea56ef831b3cf9df31c5e4f8e200e489e4 Mon Sep 17 00:00:00 2001 From: Author Name Date: Mon, 5 Jul 2021 21:19:57 +0800 Subject: [PATCH 10/33] =?UTF-8?q?=E4=BE=9D=E8=B5=96=E6=B3=A8=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\350\265\226\346\263\250\345\205\245).md" | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git "a/\346\235\250\345\256\207\346\226\214/note-2021-07-02(web API\347\254\254\345\205\255\350\212\202 \344\275\277\347\224\250\344\276\235\350\265\226\346\263\250\345\205\245).md" "b/\346\235\250\345\256\207\346\226\214/note-2021-07-02(web API\347\254\254\345\205\255\350\212\202 \344\275\277\347\224\250\344\276\235\350\265\226\346\263\250\345\205\245).md" index 54951eb..664024e 100644 --- "a/\346\235\250\345\256\207\346\226\214/note-2021-07-02(web API\347\254\254\345\205\255\350\212\202 \344\275\277\347\224\250\344\276\235\350\265\226\346\263\250\345\205\245).md" +++ "b/\346\235\250\345\256\207\346\226\214/note-2021-07-02(web API\347\254\254\345\205\255\350\212\202 \344\275\277\347\224\250\344\276\235\350\265\226\346\263\250\345\205\245).md" @@ -2,3 +2,26 @@ + dotnet restore - 恢复项目的依赖项和工具。 +1. Startup.cs +``` + public void ConfigureServices(IServiceCollection services) + { + // 注册数据库上下文到容器 + services.AddDbContext(o =>o.UseSqlServer()); + // 注册对数据库的基本操作服务到容器 + services.AddScoped(typeof(IRepository<>),typeof(EfRepository<>)); + + services.AddControllers(); + } +``` + + +2. UsersController.cs引用 +``` +private IRepository _usersRepository; + +public UserController(IRepository usersRepository) +{ + _usersRepository=usersRepository; +} +``` \ No newline at end of file -- Gitee From f6d2b3d1d5492eae218e29e01497aab055229744 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 8 Jul 2021 09:41:27 +0800 Subject: [PATCH 11/33] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=B8=80=E4=BA=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\276\235\350\265\226\346\263\250\345\205\245).md" | 12 ++++++++++-- ...234\200\344\275\263\345\256\236\350\267\265 ).md" | 1 + 2 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 "\346\235\250\345\256\207\346\226\214/note-2021-07-06(web API JWT\346\234\200\344\275\263\345\256\236\350\267\265 ).md" diff --git "a/\346\235\250\345\256\207\346\226\214/note-2021-07-02(web API\347\254\254\345\205\255\350\212\202 \344\275\277\347\224\250\344\276\235\350\265\226\346\263\250\345\205\245).md" "b/\346\235\250\345\256\207\346\226\214/note-2021-07-02(web API\347\254\254\345\205\255\350\212\202 \344\275\277\347\224\250\344\276\235\350\265\226\346\263\250\345\205\245).md" index 664024e..f58391b 100644 --- "a/\346\235\250\345\256\207\346\226\214/note-2021-07-02(web API\347\254\254\345\205\255\350\212\202 \344\275\277\347\224\250\344\276\235\350\265\226\346\263\250\345\205\245).md" +++ "b/\346\235\250\345\256\207\346\226\214/note-2021-07-02(web API\347\254\254\345\205\255\350\212\202 \344\275\277\347\224\250\344\276\235\350\265\226\346\263\250\345\205\245).md" @@ -7,16 +7,24 @@ public void ConfigureServices(IServiceCollection services) { // 注册数据库上下文到容器 - services.AddDbContext(o =>o.UseSqlServer()); + services.AddDbContext(o =>o.UseSqlServer()); // 注册对数据库的基本操作服务到容器 services.AddScoped(typeof(IRepository<>),typeof(EfRepository<>)); services.AddControllers(); } ``` +2. Dara/MyApiDb.cs +``` + // 因为我们使用AddDbContext到容器,所以此处必须得有带参数的构造函数(而且必须传递DbContextOptions类型的参数,同时父类也得调用这个参数) + public MyApiDb(DbContextOptions options):base(options) + { + } + +``` -2. UsersController.cs引用 +3. UsersController.cs引用 ``` private IRepository _usersRepository; diff --git "a/\346\235\250\345\256\207\346\226\214/note-2021-07-06(web API JWT\346\234\200\344\275\263\345\256\236\350\267\265 ).md" "b/\346\235\250\345\256\207\346\226\214/note-2021-07-06(web API JWT\346\234\200\344\275\263\345\256\236\350\267\265 ).md" new file mode 100644 index 0000000..226b95e --- /dev/null +++ "b/\346\235\250\345\256\207\346\226\214/note-2021-07-06(web API JWT\346\234\200\344\275\263\345\256\236\350\267\265 ).md" @@ -0,0 +1 @@ +# JWT最佳实践 -- Gitee From 6501ba183dfa7028d0fa286cac41a48afbc84291 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 8 Jul 2021 11:13:28 +0800 Subject: [PATCH 12/33] JWT --- ...\344\275\263\345\256\236\350\267\265 ).md" | 300 ++++++++++++++++++ 1 file changed, 300 insertions(+) diff --git "a/\346\235\250\345\256\207\346\226\214/note-2021-07-06(web API JWT\346\234\200\344\275\263\345\256\236\350\267\265 ).md" "b/\346\235\250\345\256\207\346\226\214/note-2021-07-06(web API JWT\346\234\200\344\275\263\345\256\236\350\267\265 ).md" index 226b95e..1ffa2cd 100644 --- "a/\346\235\250\345\256\207\346\226\214/note-2021-07-06(web API JWT\346\234\200\344\275\263\345\256\236\350\267\265 ).md" +++ "b/\346\235\250\345\256\207\346\226\214/note-2021-07-06(web API JWT\346\234\200\344\275\263\345\256\236\350\267\265 ).md" @@ -1 +1,301 @@ # JWT最佳实践 + +1. 安装JWT认证支持库(必须引入) +``` +dotnet add package Microsoft.AspNetCore.Authentication.JwtBearer + +dotnet add package Microsoft.AspNetCore.Authentication.JwtBearer -h +查看帮助 +``` +2. ParamModel创建TokenParameter.cs +``` +namespace MyApi.Api.ParamModel +{ + public class TokenParameter + { + //token的密钥,不能泄露,泄露意味着所有人都可以生成你的token并且访问你的服务或者接口 + public string Secret { get; set; } + + //发行人(可以是个人或组织) + public string Issuer { get; set; } + + //token的作用时间,单位为分钟,过期后需要重新获取 + public int AccessExpiration { get; set; } + + //类似一个token的东西的作用时间,单位为分钟,用于重新获取token + public int RefreshExpiration { get; set; } + } +} +``` +3. appsettings.json 生成JWT Token需要预设一些参数 +``` +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*", + "TokenParameter": { + "secret": "123456123456123456", + //secret: JWT加密的密钥16个字符以上 + + "issuer": "Root", + //issuer: 签发人的名称 + + "accessExpiration": 120, + //accessExpiration: Token的有效分钟数,过时间后Token会过期 + + "refreshExpiration": 1440 + //refreshExpiration: refreshToken的有效分钟数。过了这个时间,用户需要重新登录。 + } +} + +``` +``` +Token过期后,可以让用户重新登录认证拿Token。但这个方式会比较Low。高大上的方式是签发Token的时候,同时也签发一个refreshToken给用户。用户Token过期后,可以拿refreshToken去申请新的Token,同时刷新refreshToken。如果用户长时间未使用系统,refreshToken也过期了,才让用户重新登录认证。 + +refreshToken可以用JWT生成,也可以自己生成,不影响认证。 +``` + + +4. 生成Token 创建Utils文件夹下创建TokenHelper.cs +``` +using MyApi.Api.ParamModel; +using MyApi.Api.Entity; +using System.Security.Claims; +using Microsoft.IdentityModel.Tokens; +using System.Text; +using System.IdentityModel.Tokens.Jwt; +using System; + +namespace MyApi.Api.Utils +{ + public class TokenHelper + { + //这儿是真正的生成Token代码 + public static string GenerateToekn(TokenParameter tokenParameter,Users user) + { + var claims = new[] + { + new Claim(ClaimTypes.Name,user.Username), + new Claim(ClaimTypes.Role, "root"), + }; + + var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(tokenParameter.Secret)); + var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); + var jwtToken = new JwtSecurityToken(tokenParameter.Issuer, null, claims, expires: DateTime.UtcNow.AddMinutes(tokenParameter.AccessExpiration), signingCredentials: credentials); + + var token = new JwtSecurityTokenHandler().WriteToken(jwtToken); + + return token; + } + } +} +``` +5. Controllers/UsersController.cs +``` +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; +using MyApi.Api.Entity; +using MyApi.Api.ParamModel; +using MyApi.Api.Repository; +using MyApi.Api.Utils; + + +namespace MyApi.Api.Controllers +{ + [ApiController] + [Route("[controller]")] + public class UsersController : ControllerBase + { + // private MyApi.Api.Db.MyApiDb db = new Api.Db.MyApiDb(); + private IRepository _usersRespository; + private TokenParameter _tokenParameter; + private readonly IConfiguration _configuration; + + public UsersController(IConfiguration configuration,IRepository UsersRespository){ + _usersRespository=UsersRespository; + _configuration=configuration; + _tokenParameter=_configuration.GetSection("TokenParameter").Get(); + } + [HttpGet] + public IEnumerable Get() + { + var user = _usersRespository.Table.ToList(); + return user; + } + + [HttpGet("{id}")] + public dynamic Get(int id) + { + var user = _usersRespository.GetById(id); + if(user==null){ + return new{ + Code=1001, + Data ="", + Msg ="用户不存在", + }; + } + return new{ + Code=1000, + Data =user, + Msg ="用户以获取", + }; + } + + [HttpPost] + public dynamic post(NewUser model) + { + var user=new Users{ + Username=model.Username, + Password=model.Password, + }; + _usersRespository.Insert(user); + + + return new + { + Code=1000, + Data =user, + ParallelMergeOptions="创建成功" + }; + } + + [HttpPut("{id}")] + public dynamic Put(int id,NewUser model) + { + var user = _usersRespository.GetById(id); + + if (user != null) + { + user.Username = model.Username; + user.Password = model.Password; + user.UpdatedTime=DateTime.Now; + _usersRespository.Update(user); + return new + { + Code = 1000, + Data = user, + Msg = string.Format("你修改的用户的id为:{0},已经修改成功,请注意查收", id) + }; + } + else + { + return new + { + Code = 104, + Data = "", + Msg = "指定Id的用户不存在,请确认后重试" + }; + } + + + } + + [HttpDelete("{id}")] + public dynamic Delete(int id) + { + // var user = db.Users.Where(x => x.Id == id).FirstOrDefault(); + var user=_usersRespository.GetById(id); + if (user != null) + { + // db.Users.Remove(user); + // db.SaveChanges(); + _usersRespository.Delete(id); + return new + { + Code = 1000, + Msg = string.Format("删除指定id为{0}的用户成功", id) + }; + } + else + { + return new + { + Code = 104, + Data = "", + Msg = "指定Id的用户不存在,请确认后重试" + }; + } + } + + [HttpPost, Route("token")] + public dynamic GetToken(NewUser model) + { + var username = model.Username.Trim(); + var password = model.Password.Trim(); + + var user = _usersRespository.Table.Where(x => x.Username == username && x.Password == password).FirstOrDefault(); + + if (user == null) + { + return new + { + Code = 1001, + Data = user, + Msg = "用户名或密码不正确,请核对后重试" + }; + } + + var token = TokenHelper.GenerateToekn(_tokenParameter, user); + var refreshToken = "123456"; + + var res = new + { + Code = 1000, + Data = new { Token = token, RefreshToken = refreshToken }, + Msg = "用户登录成功,获取token成功" + }; + return res; + } + + } +} +``` +6. api.http +``` +### 获取用户 + +GET http://localhost:5000/Users HTTP/1.1 + +###获取指定id用户 +GET http://localhost:5000/Users/10 HTTP/1.1 + +###创建用户 +POST http://localhost:5000/Users HTTP/1.1 +Content-Type:application/json + +{ + "username":"123", + "password":"1113" +} + +###修改指定用户 +PUT http://localhost:5000/Users/1 HTTP/1.1 +Content-Type: application/json + +{ + "username":"666", + "password":"1113" +} + +###删除指定用户 +DELETE http://localhost:5000/Users/1 HTTP/1.1 + +### 登录并获取token +POST http://localhost:5000/Users/token HTTP/1.1 +Content-Type: application/json + +{ + "username":"123", + "password":"1113" +} +``` \ No newline at end of file -- Gitee From 6fdb8def94fc0050be647c45558b25bd7f16b295 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 8 Jul 2021 16:15:28 +0800 Subject: [PATCH 13/33] =?UTF-8?q?JWT=E6=9C=80=E4=BD=B3=E5=AE=9E=E8=B7=B5?= =?UTF-8?q?=20=E5=92=8C=20=E6=97=A5=E5=BF=97=E5=AE=A1=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\214 \346\227\245\345\277\227\345\256\241\350\256\241).md" | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 "\346\235\250\345\256\207\346\226\214/note-2021-07-08(web API JWT\346\234\200\344\275\263\345\256\236\350\267\265 \345\222\214 \346\227\245\345\277\227\345\256\241\350\256\241).md" diff --git "a/\346\235\250\345\256\207\346\226\214/note-2021-07-08(web API JWT\346\234\200\344\275\263\345\256\236\350\267\265 \345\222\214 \346\227\245\345\277\227\345\256\241\350\256\241).md" "b/\346\235\250\345\256\207\346\226\214/note-2021-07-08(web API JWT\346\234\200\344\275\263\345\256\236\350\267\265 \345\222\214 \346\227\245\345\277\227\345\256\241\350\256\241).md" new file mode 100644 index 0000000..239d67e --- /dev/null +++ "b/\346\235\250\345\256\207\346\226\214/note-2021-07-08(web API JWT\346\234\200\344\275\263\345\256\236\350\267\265 \345\222\214 \346\227\245\345\277\227\345\256\241\350\256\241).md" @@ -0,0 +1,4 @@ +# JWT最佳实践 和 日志审计 + +## JWT最佳实践(二) +1. \ No newline at end of file -- Gitee From f577db3e2a40583729ab2ac65566b84f713d27b0 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 8 Jul 2021 16:26:44 +0800 Subject: [PATCH 14/33] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...3\351\227\264\347\232\204\345\205\263\347\263\273).md" | 0 ...\344\272\224\350\212\202 \346\216\245\345\217\243).md" | 2 +- ... \346\227\245\345\277\227\345\256\241\350\256\241).md" | 8 +++++++- 3 files changed, 8 insertions(+), 2 deletions(-) rename "\346\235\250\345\256\207\346\226\214/note-2021-06-29(web API\347\254\254\345\233\233\350\212\202).md" => "\346\235\250\345\256\207\346\226\214/note-2021-06-29(web API\347\254\254\345\233\233\350\212\202 \345\273\272\347\253\213\350\241\250\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273).md" (100%) diff --git "a/\346\235\250\345\256\207\346\226\214/note-2021-06-29(web API\347\254\254\345\233\233\350\212\202).md" "b/\346\235\250\345\256\207\346\226\214/note-2021-06-29(web API\347\254\254\345\233\233\350\212\202 \345\273\272\347\253\213\350\241\250\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273).md" similarity index 100% rename from "\346\235\250\345\256\207\346\226\214/note-2021-06-29(web API\347\254\254\345\233\233\350\212\202).md" rename to "\346\235\250\345\256\207\346\226\214/note-2021-06-29(web API\347\254\254\345\233\233\350\212\202 \345\273\272\347\253\213\350\241\250\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273).md" diff --git "a/\346\235\250\345\256\207\346\226\214/note-2021-06-30(web API \347\254\254\344\272\224\350\212\202 \346\216\245\345\217\243).md" "b/\346\235\250\345\256\207\346\226\214/note-2021-06-30(web API \347\254\254\344\272\224\350\212\202 \346\216\245\345\217\243).md" index 20c4d35..651db60 100644 --- "a/\346\235\250\345\256\207\346\226\214/note-2021-06-30(web API \347\254\254\344\272\224\350\212\202 \346\216\245\345\217\243).md" +++ "b/\346\235\250\345\256\207\346\226\214/note-2021-06-30(web API \347\254\254\344\272\224\350\212\202 \346\216\245\345\217\243).md" @@ -1,4 +1,4 @@ -# 2021-6-29 web API 第五节 接口 +# web API 第五节 接口 + IQueryable 是将Skip ,take 这些方法表达式翻译成T-SQL语句之后再向SQL服务器发送命令,它并不是把所有数据都加载到内存里来才进行条件过滤。 + interface 接口 ``` diff --git "a/\346\235\250\345\256\207\346\226\214/note-2021-07-08(web API JWT\346\234\200\344\275\263\345\256\236\350\267\265 \345\222\214 \346\227\245\345\277\227\345\256\241\350\256\241).md" "b/\346\235\250\345\256\207\346\226\214/note-2021-07-08(web API JWT\346\234\200\344\275\263\345\256\236\350\267\265 \345\222\214 \346\227\245\345\277\227\345\256\241\350\256\241).md" index 239d67e..2621700 100644 --- "a/\346\235\250\345\256\207\346\226\214/note-2021-07-08(web API JWT\346\234\200\344\275\263\345\256\236\350\267\265 \345\222\214 \346\227\245\345\277\227\345\256\241\350\256\241).md" +++ "b/\346\235\250\345\256\207\346\226\214/note-2021-07-08(web API JWT\346\234\200\344\275\263\345\256\236\350\267\265 \345\222\214 \346\227\245\345\277\227\345\256\241\350\256\241).md" @@ -1,4 +1,10 @@ # JWT最佳实践 和 日志审计 ## JWT最佳实践(二) -1. \ No newline at end of file +1. Token认证 在Startup.cs中,ConfigureServices方法里,添加以下内容 +``` + +``` +2. 在Startup.cs里,Configure方法中,打开认证 + +3. 设置API认证。 -- Gitee From f997032ecb9a57c948933b58bd62cedf62c90d35 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 8 Jul 2021 17:36:42 +0800 Subject: [PATCH 15/33] =?UTF-8?q?Token=E8=AE=A4=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\350\265\226\346\263\250\345\205\245).md" | 21 +++++++++++++++++++ ...5\345\277\227\345\256\241\350\256\241).md" | 13 ++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git "a/\346\235\250\345\256\207\346\226\214/note-2021-07-02(web API\347\254\254\345\205\255\350\212\202 \344\275\277\347\224\250\344\276\235\350\265\226\346\263\250\345\205\245).md" "b/\346\235\250\345\256\207\346\226\214/note-2021-07-02(web API\347\254\254\345\205\255\350\212\202 \344\275\277\347\224\250\344\276\235\350\265\226\346\263\250\345\205\245).md" index f58391b..c18a85e 100644 --- "a/\346\235\250\345\256\207\346\226\214/note-2021-07-02(web API\347\254\254\345\205\255\350\212\202 \344\275\277\347\224\250\344\276\235\350\265\226\346\263\250\345\205\245).md" +++ "b/\346\235\250\345\256\207\346\226\214/note-2021-07-02(web API\347\254\254\345\205\255\350\212\202 \344\275\277\347\224\250\344\276\235\350\265\226\346\263\250\345\205\245).md" @@ -11,6 +11,27 @@ // 注册对数据库的基本操作服务到容器 services.AddScoped(typeof(IRepository<>),typeof(EfRepository<>)); + // 注册验证器(使用何种配置来验证token) + services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) + .AddJwtBearer(option => + { + // 是否要求使用https + option.RequireHttpsMetadata=false; + // 是否保存token + option.SaveToken=true; + // 使用配置中间件,获得token的配置 + var tokenParameter=Configuration.GetSection("TokenParameter").Get(); + // 验证器的核心属性 + option.TokenValidationParameters=new TokenValidationParameters + { + ValidateIssuerSigningKey=true,//要不要验证生成token的密钥 + IssuerSigningKey=new SymmetricSecurityKey(Encoding.UTF8.GetBytes(tokenParameter.Secret)), + ValidateIssuer=true, // 要不要验证发行token的人(个人或者组织) + ValidIssuer=tokenParameter.Issuer, + ValidateAudience=false // 是否验证受众 + }; + }); + services.AddControllers(); } ``` diff --git "a/\346\235\250\345\256\207\346\226\214/note-2021-07-08(web API JWT\346\234\200\344\275\263\345\256\236\350\267\265 \345\222\214 \346\227\245\345\277\227\345\256\241\350\256\241).md" "b/\346\235\250\345\256\207\346\226\214/note-2021-07-08(web API JWT\346\234\200\344\275\263\345\256\236\350\267\265 \345\222\214 \346\227\245\345\277\227\345\256\241\350\256\241).md" index 2621700..09d9afc 100644 --- "a/\346\235\250\345\256\207\346\226\214/note-2021-07-08(web API JWT\346\234\200\344\275\263\345\256\236\350\267\265 \345\222\214 \346\227\245\345\277\227\345\256\241\350\256\241).md" +++ "b/\346\235\250\345\256\207\346\226\214/note-2021-07-08(web API JWT\346\234\200\344\275\263\345\256\236\350\267\265 \345\222\214 \346\227\245\345\277\227\345\256\241\350\256\241).md" @@ -1,10 +1,19 @@ # JWT最佳实践 和 日志审计 ## JWT最佳实践(二) -1. Token认证 在Startup.cs中,ConfigureServices方法里,添加以下内容 ++ Token认证 + +1. 在Startup.cs中,ConfigureServices方法里,添加以下内容 ``` ``` 2. 在Startup.cs里,Configure方法中,打开认证 - +``` +app.UseAuthentication(); +app.UseAuthorization(); +``` 3. 设置API认证。 + + + +1. 创建Filters文件夹下创建AuditLogActionFilter.cs -- Gitee From 5463eeee989cc6d7c086fe68211a536b4531f325 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 9 Jul 2021 08:18:28 +0800 Subject: [PATCH 16/33] =?UTF-8?q?JWT=E6=9C=80=E4=BD=B3=E5=AE=9E=E8=B7=B5?= =?UTF-8?q?=20=E5=92=8C=20=E6=97=A5=E5=BF=97=E5=AE=A1=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\345\277\227\345\256\241\350\256\241).md" | 51 ++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git "a/\346\235\250\345\256\207\346\226\214/note-2021-07-08(web API JWT\346\234\200\344\275\263\345\256\236\350\267\265 \345\222\214 \346\227\245\345\277\227\345\256\241\350\256\241).md" "b/\346\235\250\345\256\207\346\226\214/note-2021-07-08(web API JWT\346\234\200\344\275\263\345\256\236\350\267\265 \345\222\214 \346\227\245\345\277\227\345\256\241\350\256\241).md" index 09d9afc..41d8039 100644 --- "a/\346\235\250\345\256\207\346\226\214/note-2021-07-08(web API JWT\346\234\200\344\275\263\345\256\236\350\267\265 \345\222\214 \346\227\245\345\277\227\345\256\241\350\256\241).md" +++ "b/\346\235\250\345\256\207\346\226\214/note-2021-07-08(web API JWT\346\234\200\344\275\263\345\256\236\350\267\265 \345\222\214 \346\227\245\345\277\227\345\256\241\350\256\241).md" @@ -5,15 +5,64 @@ 1. 在Startup.cs中,ConfigureServices方法里,添加以下内容 ``` + // 注册数据库上下文到容器 + services.AddDbContext(o =>o.UseSqlServer()); + // 注册对数据库的基本操作服务到容器 + services.AddScoped(typeof(IRepository<>),typeof(EfRepository<>)); + // 注册验证器(使用何种配置来验证token) + services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) + .AddJwtBearer(option => + { + // 是否要求使用https + option.RequireHttpsMetadata=false; + // 是否保存token + option.SaveToken=true; + // 使用配置中间件,获得token的配置 + var tokenParameter=Configuration.GetSection("TokenParameter").Get(); + // 验证器的核心属性 + option.TokenValidationParameters=new TokenValidationParameters + { + ValidateIssuerSigningKey=true,//要不要验证生成token的密钥 + IssuerSigningKey=new SymmetricSecurityKey(Encoding.UTF8.GetBytes(tokenParameter.Secret)), + ValidateIssuer=true, // 要不要验证发行token的人(个人或者组织) + ValidIssuer=tokenParameter.Issuer, + ValidateAudience=false // 是否验证受众 + }; + }); + services.AddControllers(); ``` 2. 在Startup.cs里,Configure方法中,打开认证 ``` +// 将token的验证注册到中间件 app.UseAuthentication(); app.UseAuthorization(); ``` -3. 设置API认证。 +3. 设置API认证。 在UsersController.cs下的Get方法来测试 ++ 在方法前边,我们加上Authorize +``` + [Authorize] + [ApiController] + [Route("[controller]")] + public class UsersController : ControllerBase{} +``` +4. 认证后的访问 +``` +api.http +### 获取用户 +GET http://localhost:5000/Users HTTP/1.1 +Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoicm9vdCIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvcm9sZSI6InJvb3QiLCJleHAiOjE2MjU3NDQwNTIsImlzcyI6IlJvb3QifQ.C_aUR4JDPl-2r6Cccv-BrNnmJgYG7OhJhq6VbOfB5l4 +### 登录并获取token +POST http://localhost:5000/Users/token HTTP/1.1 +Content-Type: application/json +{ + "username":"root", + "password":"1113" +} +``` +# 日志审计 1. 创建Filters文件夹下创建AuditLogActionFilter.cs + -- Gitee From 79939eb8471f3403d2de6038791f5ebebda82277 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 9 Jul 2021 08:47:57 +0800 Subject: [PATCH 17/33] =?UTF-8?q?JWT=E6=9C=80=E4=BD=B3=E5=AE=9E=E8=B7=B5?= =?UTF-8?q?=20=E5=92=8C=20=E6=97=A5=E5=BF=97=E5=AE=A1=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\345\277\227\345\256\241\350\256\241).md" | 60 ++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git "a/\346\235\250\345\256\207\346\226\214/note-2021-07-08(web API JWT\346\234\200\344\275\263\345\256\236\350\267\265 \345\222\214 \346\227\245\345\277\227\345\256\241\350\256\241).md" "b/\346\235\250\345\256\207\346\226\214/note-2021-07-08(web API JWT\346\234\200\344\275\263\345\256\236\350\267\265 \345\222\214 \346\227\245\345\277\227\345\256\241\350\256\241).md" index 41d8039..ab69d28 100644 --- "a/\346\235\250\345\256\207\346\226\214/note-2021-07-08(web API JWT\346\234\200\344\275\263\345\256\236\350\267\265 \345\222\214 \346\227\245\345\277\227\345\256\241\350\256\241).md" +++ "b/\346\235\250\345\256\207\346\226\214/note-2021-07-08(web API JWT\346\234\200\344\275\263\345\256\236\350\267\265 \345\222\214 \346\227\245\345\277\227\345\256\241\350\256\241).md" @@ -64,5 +64,63 @@ Content-Type: application/json } ``` # 日志审计 -1. 创建Filters文件夹下创建AuditLogActionFilter.cs +1. 定义审计日志信息 在Entity 下创建AuditInfo.cs +``` +public class AuditInfo +{ + /// + /// 调用参数 + /// + public string Parameters { get; set; } + /// + /// 浏览器信息 + /// + public string BrowserInfo { get; set; } + /// + /// 客户端信息 + /// + public string ClientName { get; set; } + /// + /// 客户端IP地址 + /// + public string ClientIpAddress { get; set; } + /// + /// 执行耗时 + /// + public int ExecutionDuration { get; set; } + /// + /// 执行时间 + /// + public DateTime ExecutionTime { get; set; } + /// + /// 返回内容 + /// + public string ReturnValue { get; set; } + /// + /// 异常对象 + /// + public string Exception { get; set; } + /// + /// 方法名 + /// + public string MethodName { get; set; } + /// + /// 服务名 + /// + public string ServiceName { get; set; } + /// + /// 调用者信息 + /// + public string UserInfo { get; set; } + /// + /// 自定义数据 + /// + public string CustomData { get; set; } +} +``` +2. 实现审计日志过滤器 创建Filters文件夹下创建AuditLogActionFilter.cs +``` + +``` + -- Gitee From aabc3001f12520415f0437176906809e4b3e5115 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 9 Jul 2021 08:51:56 +0800 Subject: [PATCH 18/33] ef core --- ...l\347\254\254\344\270\211\350\212\202\350\257\276).md" | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git "a/\346\235\250\345\256\207\346\226\214/note-2021-06-26(web APl\347\254\254\344\270\211\350\212\202\350\257\276).md" "b/\346\235\250\345\256\207\346\226\214/note-2021-06-26(web APl\347\254\254\344\270\211\350\212\202\350\257\276).md" index 0b904e5..5e4a8ed 100644 --- "a/\346\235\250\345\256\207\346\226\214/note-2021-06-26(web APl\347\254\254\344\270\211\350\212\202\350\257\276).md" +++ "b/\346\235\250\345\256\207\346\226\214/note-2021-06-26(web APl\347\254\254\344\270\211\350\212\202\350\257\276).md" @@ -6,8 +6,8 @@ dotnet add package Microsoft.EntityFrameworkCore.Sqlserver //数据库为sqlserver ``` -2. 在项目文件夹中,创建Entity文件夹,添加User.cs,Student.cs等 定义字段 -3. 在项目文件夹中,创建Data文件夹,添加myApiDb.cs +2. 在项目文件夹中,创建Entity文件夹,添加User.cs,Student.cs等 定义字段,实体类 +3.定义构成模型的上下文类。 在项目文件夹中,创建Data文件夹,添加myApiDb.cs ``` using MyApi.Api.Entity; using Microsoft.EntityFrameworkCore; @@ -25,9 +25,13 @@ namespace MyApi.Api.Db ``` 4. 创建数据库 使用迁移创建数据库 ``` +//安装 dotnet ef dotnet tool install --global dotnet-ef +//安装设计包 dotnet add package Microsoft.EntityFrameworkCore.Design +//迁移搭建基架,以便为模型创建一组初始表 dotnet ef migrations add InitialCreate +//命令创建数据库并向其应用新的迁移 dotnet ef database update ``` 5. 在项目文件夹中,创建ParamModel文件夹,添加newUser.cs 模型 -- Gitee From 7247863359dcafc128818e16e2efa002af206e3c Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 9 Jul 2021 09:51:20 +0800 Subject: [PATCH 19/33] =?UTF-8?q?JWT=E6=9C=80=E4=BD=B3=E5=AE=9E=E8=B7=B5?= =?UTF-8?q?=20=E5=92=8C=20=E6=97=A5=E5=BF=97=E5=AE=A1=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\345\277\227\345\256\241\350\256\241).md" | 158 +++++++++++++++++- 1 file changed, 157 insertions(+), 1 deletion(-) diff --git "a/\346\235\250\345\256\207\346\226\214/note-2021-07-08(web API JWT\346\234\200\344\275\263\345\256\236\350\267\265 \345\222\214 \346\227\245\345\277\227\345\256\241\350\256\241).md" "b/\346\235\250\345\256\207\346\226\214/note-2021-07-08(web API JWT\346\234\200\344\275\263\345\256\236\350\267\265 \345\222\214 \346\227\245\345\277\227\345\256\241\350\256\241).md" index ab69d28..caa986d 100644 --- "a/\346\235\250\345\256\207\346\226\214/note-2021-07-08(web API JWT\346\234\200\344\275\263\345\256\236\350\267\265 \345\222\214 \346\227\245\345\277\227\345\256\241\350\256\241).md" +++ "b/\346\235\250\345\256\207\346\226\214/note-2021-07-08(web API JWT\346\234\200\344\275\263\345\256\236\350\267\265 \345\222\214 \346\227\245\345\277\227\345\256\241\350\256\241).md" @@ -120,7 +120,163 @@ public class AuditInfo ``` 2. 实现审计日志过滤器 创建Filters文件夹下创建AuditLogActionFilter.cs ``` +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Controllers; +using Microsoft.AspNetCore.Mvc.Filters; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Reflection; +using System.Threading.Tasks; +using MyApi.Api.Repository; +using MyApi.Api.Entity; -``` +namespace MyApi.Api.Filters +{ + public class AuditLogActionFilter : IAsyncActionFilter + { + + private readonly IRepository _auditLogService; + + public AuditLogActionFilter( + IRepository auditLogService + ) + { + _auditLogService=auditLogService; + } + + public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) + { + // 判断是否写日志 + if (!ShouldSaveAudit(context)) + { + await next(); + return; + } + //接口Type + var type = (context.ActionDescriptor as ControllerActionDescriptor).ControllerTypeInfo.AsType(); + //方法信息 + var method = (context.ActionDescriptor as ControllerActionDescriptor).MethodInfo; + //方法参数 + var arguments = context.ActionArguments; + //开始计时 + var stopwatch = Stopwatch.StartNew(); + var auditInfo = new AuditInfo + { + UserInfo = "小明", + ServiceName = type != null ? type.FullName : "", + MethodName = method.Name, + ////请求参数转Json + Parameters = JsonConvert.SerializeObject(arguments), + ExecutionTime = DateTime.Now, + BrowserInfo = context.HttpContext.Request.Headers["User-Agent"].ToString(), + ClientIpAddress = context.HttpContext.Connection.RemoteIpAddress.ToString(), + //ClientName = _clientInfoProvider.ComputerName.TruncateWithPostfix(EntityDefault.FieldsLength100), + // Id = Guid.NewGuid().ToString() + }; + + ActionExecutedContext result = null; + try + { + result = await next(); + if (result.Exception != null && !result.ExceptionHandled) + { + auditInfo.Exception = result.Exception.ToString(); + } + } + catch (Exception ex) + { + auditInfo.Exception = ex.ToString(); + throw; + } + finally + { + stopwatch.Stop(); + auditInfo.ExecutionDuration = Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds); + + if (result != null) + { + switch (result.Result) + { + case ObjectResult objectResult: + auditInfo.ReturnValue = JsonConvert.SerializeObject(objectResult.Value); + break; + + case JsonResult jsonResult: + auditInfo.ReturnValue = JsonConvert.SerializeObject(jsonResult.Value); + break; + case ContentResult contentResult: + auditInfo.ReturnValue = contentResult.Content; + break; + } + } + Console.WriteLine(auditInfo.ToString()); + //保存审计日志 + await _auditLogService.InsertAsync(auditInfo); + } + } + + /// + /// 是否需要记录审计 + /// + /// + /// + private bool ShouldSaveAudit(ActionExecutingContext context) + { + if (!(context.ActionDescriptor is ControllerActionDescriptor)) + return false; + var methodInfo = (context.ActionDescriptor as ControllerActionDescriptor).MethodInfo; + + if (methodInfo == null) + { + return false; + } + + if (!methodInfo.IsPublic) + { + return false; + } + + //if (methodInfo.GetCustomAttribute() != null) + //{ + // return true; + //} + + //if (methodInfo.GetCustomAttribute() != null) + //{ + // return false; + //} + + //var classType = methodInfo.DeclaringType; + //if (classType != null) + //{ + // if (classType.GetTypeInfo().GetCustomAttribute() != null) + // { + // return true; + // } + + // if (classType.GetTypeInfo().GetCustomAttribute() != null) + // { + // return false; + // } + // } + return true; + } + } +} +``` +3. 注册过滤器 Startup.cs +``` + public void ConfigureServices(IServiceCollection services) + { + services.AddControllers(options=>{ + options.Filters.Add(typeof(AuditLogActionFilter)); + }); + } +``` -- Gitee From ac21203d123b27a625f68d581853a746db3a1f7a Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 10 Jul 2021 15:51:29 +0800 Subject: [PATCH 20/33] =?UTF-8?q?vue=20cli=20=E5=AE=89=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...02\350\257\276 vue Cli \345\256\211\350\243\205 ).md" | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git "a/\346\235\250\345\256\207\346\226\214/note-2021-06-09(\347\254\254\345\215\201\350\212\202\350\257\276 vue Cli \345\256\211\350\243\205 ).md" "b/\346\235\250\345\256\207\346\226\214/note-2021-06-09(\347\254\254\345\215\201\350\212\202\350\257\276 vue Cli \345\256\211\350\243\205 ).md" index 41581f9..d327566 100644 --- "a/\346\235\250\345\256\207\346\226\214/note-2021-06-09(\347\254\254\345\215\201\350\212\202\350\257\276 vue Cli \345\256\211\350\243\205 ).md" +++ "b/\346\235\250\345\256\207\346\226\214/note-2021-06-09(\347\254\254\345\215\201\350\212\202\350\257\276 vue Cli \345\256\211\350\243\205 ).md" @@ -5,10 +5,17 @@ ``` npm i -g yarn ``` -2. 安装yarn淘宝镜像 +2. 安装淘宝镜像 ``` +npm npm config set registry https://registry.npm.taobao.org yarn config set registry https://registry.npm.taobao.org/ ``` +``` +配置后可通过下面方式来验证是否成功 +npm config get registry +或 +npm info express +``` 3. 安装 vue cli ``` npm install -g @vue/cli -- Gitee From a67c6c9e37c85f4f7f95e8611f66e667ff6b8c8a Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 10 Jul 2021 17:33:54 +0800 Subject: [PATCH 21/33] Element --- "\346\235\250\345\256\207\346\226\214/note-2021-07-10.md" | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 "\346\235\250\345\256\207\346\226\214/note-2021-07-10.md" diff --git "a/\346\235\250\345\256\207\346\226\214/note-2021-07-10.md" "b/\346\235\250\345\256\207\346\226\214/note-2021-07-10.md" new file mode 100644 index 0000000..a225b3b --- /dev/null +++ "b/\346\235\250\345\256\207\346\226\214/note-2021-07-10.md" @@ -0,0 +1,7 @@ +## Element +1. 安装 +``` +npm i element-ui -S +yarn add element-ui +``` +ll -al -- Gitee From 423744df86e749d243570e45607fd01bd8f8c04d Mon Sep 17 00:00:00 2001 From: yang <2385659228@qq.com> Date: Tue, 13 Jul 2021 10:53:40 +0800 Subject: [PATCH 22/33] Vue Cli Element --- .../note-2021-07-10(Vue Cli Element).md" | 147 ++++++++++++++++++ .../note-2021-07-10.md" | 7 - 2 files changed, 147 insertions(+), 7 deletions(-) create mode 100644 "\346\235\250\345\256\207\346\226\214/note-2021-07-10(Vue Cli Element).md" delete mode 100644 "\346\235\250\345\256\207\346\226\214/note-2021-07-10.md" diff --git "a/\346\235\250\345\256\207\346\226\214/note-2021-07-10(Vue Cli Element).md" "b/\346\235\250\345\256\207\346\226\214/note-2021-07-10(Vue Cli Element).md" new file mode 100644 index 0000000..1fa4bc5 --- /dev/null +++ "b/\346\235\250\345\256\207\346\226\214/note-2021-07-10(Vue Cli Element).md" @@ -0,0 +1,147 @@ +## Element +1. 安装 +``` +npm i element-ui -S +yarn add element-ui +``` +2. 包装router +``` +在src下创建 router文件夹 + +index.js + +import Vue from 'vue' +import VueRouter from 'vue-router' +import routes from './routes' +Vue.use(VueRouter) + +let router= new VueRouter({ + mode:'history', + routes +}) + +export default router + + +routes.js +import Layout from '../components/Layout.vue' + +let routes=[{ + path:'/', + component:Layout, + children:[{ + path:'home', + component:()=>import('../components/Home') + }, + { + path:'user', + component:()=>import('../components/User') + } +] +}] +export default routes +``` +3. main.js +``` +import Vue from 'vue' +import App from './App.vue' +import router from './router' +import ElementUI from 'element-ui'; +import 'element-ui/lib/theme-chalk/index.css'; + +Vue.use(ElementUI); +Vue.config.productionTip = false + +new Vue({ + router, + render: h => h(App), +}).$mount('#app') + +``` +4. src/components 下创建 Layout.js User.js Home.js ++ router 是否使用 vue-router 的模式,启用该模式会在激活导航时以 index 作为 path 进行路由跳转 ++ route Vue Router 路径对象 +``` +Layout.js + + + + +``` +ll -al diff --git "a/\346\235\250\345\256\207\346\226\214/note-2021-07-10.md" "b/\346\235\250\345\256\207\346\226\214/note-2021-07-10.md" deleted file mode 100644 index a225b3b..0000000 --- "a/\346\235\250\345\256\207\346\226\214/note-2021-07-10.md" +++ /dev/null @@ -1,7 +0,0 @@ -## Element -1. 安装 -``` -npm i element-ui -S -yarn add element-ui -``` -ll -al -- Gitee From 49dcfe084655cf9b00b61a744dce8418f31af4b9 Mon Sep 17 00:00:00 2001 From: yang <2385659228@qq.com> Date: Wed, 14 Jul 2021 11:11:21 +0800 Subject: [PATCH 23/33] =?UTF-8?q?=E7=AE=80=E5=8D=95=E7=9A=84=E6=9E=84?= =?UTF-8?q?=E9=80=A0=E8=B7=AF=E7=94=B1=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7\347\224\261\346\225\260\346\215\256).md" | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 "\346\235\250\345\256\207\346\226\214/note-2021-07-13(\346\236\204\351\200\240\350\267\257\347\224\261\346\225\260\346\215\256).md" diff --git "a/\346\235\250\345\256\207\346\226\214/note-2021-07-13(\346\236\204\351\200\240\350\267\257\347\224\261\346\225\260\346\215\256).md" "b/\346\235\250\345\256\207\346\226\214/note-2021-07-13(\346\236\204\351\200\240\350\267\257\347\224\261\346\225\260\346\215\256).md" new file mode 100644 index 0000000..8594ccb --- /dev/null +++ "b/\346\235\250\345\256\207\346\226\214/note-2021-07-13(\346\236\204\351\200\240\350\267\257\347\224\261\346\225\260\346\215\256).md" @@ -0,0 +1,48 @@ +# 构造路由数据 +1. 简单的构造路由数据 +``` +Layout.vue + + +
+ + + +
+ + + + + + {{ child.title }} + +
+
+ + + + {{ menu.title }} + +
+ + +``` \ No newline at end of file -- Gitee From 32d343fa68043339f9959ef69753c9f2b9c474a5 Mon Sep 17 00:00:00 2001 From: yang <2385659228@qq.com> Date: Thu, 15 Jul 2021 08:55:21 +0800 Subject: [PATCH 24/33] =?UTF-8?q?=E6=9E=84=E9=80=A0=E8=B7=AF=E7=94=B1?= =?UTF-8?q?=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7\347\224\261\346\225\260\346\215\256).md" | 164 ++++++++++++++++++ ...7\347\224\261\350\267\257\345\276\204).md" | 0 2 files changed, 164 insertions(+) create mode 100644 "\346\235\250\345\256\207\346\226\214/note-2021-07-15(\351\201\215\345\216\206\345\207\272\350\267\257\347\224\261\350\267\257\345\276\204).md" diff --git "a/\346\235\250\345\256\207\346\226\214/note-2021-07-13(\346\236\204\351\200\240\350\267\257\347\224\261\346\225\260\346\215\256).md" "b/\346\235\250\345\256\207\346\226\214/note-2021-07-13(\346\236\204\351\200\240\350\267\257\347\224\261\346\225\260\346\215\256).md" index 8594ccb..9220252 100644 --- "a/\346\235\250\345\256\207\346\226\214/note-2021-07-13(\346\236\204\351\200\240\350\267\257\347\224\261\346\225\260\346\215\256).md" +++ "b/\346\235\250\345\256\207\346\226\214/note-2021-07-13(\346\236\204\351\200\240\350\267\257\347\224\261\346\225\260\346\215\256).md" @@ -45,4 +45,168 @@ Layout.vue }, }; +``` +2. 包装路由数据 ++ components下创建Navbar文件夹 +``` +index.vue + + + + + +``` +``` +Navbarltem.vue + + + + + +``` +``` +collapse 是否水平折叠收起菜单(仅在 mode 为 vertical 时可用) +Layout.js + + + + ``` \ No newline at end of file diff --git "a/\346\235\250\345\256\207\346\226\214/note-2021-07-15(\351\201\215\345\216\206\345\207\272\350\267\257\347\224\261\350\267\257\345\276\204).md" "b/\346\235\250\345\256\207\346\226\214/note-2021-07-15(\351\201\215\345\216\206\345\207\272\350\267\257\347\224\261\350\267\257\345\276\204).md" new file mode 100644 index 0000000..e69de29 -- Gitee From 8ad370c0abfbc5962b39bebc423164262a4fadb5 Mon Sep 17 00:00:00 2001 From: yang <2385659228@qq.com> Date: Thu, 15 Jul 2021 09:23:39 +0800 Subject: [PATCH 25/33] =?UTF-8?q?=E6=9E=84=E9=80=A0=E8=B7=AF=E7=94=B1?= =?UTF-8?q?=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...200\240\350\267\257\347\224\261\346\225\260\346\215\256).md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/\346\235\250\345\256\207\346\226\214/note-2021-07-13(\346\236\204\351\200\240\350\267\257\347\224\261\346\225\260\346\215\256).md" "b/\346\235\250\345\256\207\346\226\214/note-2021-07-13(\346\236\204\351\200\240\350\267\257\347\224\261\346\225\260\346\215\256).md" index 9220252..679b1b9 100644 --- "a/\346\235\250\345\256\207\346\226\214/note-2021-07-13(\346\236\204\351\200\240\350\267\257\347\224\261\346\225\260\346\215\256).md" +++ "b/\346\235\250\345\256\207\346\226\214/note-2021-07-13(\346\236\204\351\200\240\350\267\257\347\224\261\346\225\260\346\215\256).md" @@ -139,7 +139,7 @@ Layout.js
- + -- Gitee From 072d8108ce260d6ac4abb05b2304fb3f69579c04 Mon Sep 17 00:00:00 2001 From: yang <2385659228@qq.com> Date: Thu, 15 Jul 2021 09:35:46 +0800 Subject: [PATCH 26/33] =?UTF-8?q?=E6=9E=84=E9=80=A0=E8=B7=AF=E7=94=B1?= =?UTF-8?q?=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\350\267\257\347\224\261\346\225\260\346\215\256).md" | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git "a/\346\235\250\345\256\207\346\226\214/note-2021-07-13(\346\236\204\351\200\240\350\267\257\347\224\261\346\225\260\346\215\256).md" "b/\346\235\250\345\256\207\346\226\214/note-2021-07-13(\346\236\204\351\200\240\350\267\257\347\224\261\346\225\260\346\215\256).md" index 679b1b9..c1602f1 100644 --- "a/\346\235\250\345\256\207\346\226\214/note-2021-07-13(\346\236\204\351\200\240\350\267\257\347\224\261\346\225\260\346\215\256).md" +++ "b/\346\235\250\345\256\207\346\226\214/note-2021-07-13(\346\236\204\351\200\240\350\267\257\347\224\261\346\225\260\346\215\256).md" @@ -51,8 +51,8 @@ Layout.vue ``` index.vue @@ -66,6 +66,10 @@ export default { menus:{ typr:Array, require:true, + }, + collapse:{ + type:Boolean, + default:false, } } } @@ -74,6 +78,7 @@ export default { -- Gitee From c2e1c171985a14c084392d3ec9029d70b2ac83de Mon Sep 17 00:00:00 2001 From: yang <2385659228@qq.com> Date: Thu, 15 Jul 2021 09:39:37 +0800 Subject: [PATCH 27/33] =?UTF-8?q?=E6=9E=84=E9=80=A0=E8=B7=AF=E7=94=B1?= =?UTF-8?q?=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...240\350\267\257\347\224\261\346\225\260\346\215\256).md" | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git "a/\346\235\250\345\256\207\346\226\214/note-2021-07-13(\346\236\204\351\200\240\350\267\257\347\224\261\346\225\260\346\215\256).md" "b/\346\235\250\345\256\207\346\226\214/note-2021-07-13(\346\236\204\351\200\240\350\267\257\347\224\261\346\225\260\346\215\256).md" index c1602f1..60946d8 100644 --- "a/\346\235\250\345\256\207\346\226\214/note-2021-07-13(\346\236\204\351\200\240\350\267\257\347\224\261\346\225\260\346\215\256).md" +++ "b/\346\235\250\345\256\207\346\226\214/note-2021-07-13(\346\236\204\351\200\240\350\267\257\347\224\261\346\225\260\346\215\256).md" @@ -134,7 +134,11 @@ export default { }; - ``` ``` -- Gitee From 0a9b893833346dcff23585076592e4c800a44536 Mon Sep 17 00:00:00 2001 From: yang <2385659228@qq.com> Date: Thu, 15 Jul 2021 10:51:14 +0800 Subject: [PATCH 28/33] =?UTF-8?q?=E9=81=8D=E5=8E=86=E5=87=BA=E8=B7=AF?= =?UTF-8?q?=E7=94=B1=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7\347\224\261\350\267\257\345\276\204).md" | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git "a/\346\235\250\345\256\207\346\226\214/note-2021-07-15(\351\201\215\345\216\206\345\207\272\350\267\257\347\224\261\350\267\257\345\276\204).md" "b/\346\235\250\345\256\207\346\226\214/note-2021-07-15(\351\201\215\345\216\206\345\207\272\350\267\257\347\224\261\350\267\257\345\276\204).md" index e69de29..3aa4662 100644 --- "a/\346\235\250\345\256\207\346\226\214/note-2021-07-15(\351\201\215\345\216\206\345\207\272\350\267\257\347\224\261\350\267\257\345\276\204).md" +++ "b/\346\235\250\345\256\207\346\226\214/note-2021-07-15(\351\201\215\345\216\206\345\207\272\350\267\257\347\224\261\350\267\257\345\276\204).md" @@ -0,0 +1,41 @@ +# 遍历出路由路径 +1. Layout.vue +``` + +``` \ No newline at end of file -- Gitee From 9b4fb25f2459c5323bfa1796a11e753639df5f50 Mon Sep 17 00:00:00 2001 From: yang <2385659228@qq.com> Date: Fri, 16 Jul 2021 16:17:50 +0800 Subject: [PATCH 29/33] =?UTF-8?q?=E9=81=8D=E5=8E=86=E8=B7=AF=E7=94=B1?= =?UTF-8?q?=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\207\272\350\267\257\347\224\261\350\267\257\345\276\204).md" | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename "\346\235\250\345\256\207\346\226\214/note-2021-07-15(\351\201\215\345\216\206\345\207\272\350\267\257\347\224\261\350\267\257\345\276\204).md" => "\346\235\250\345\256\207\346\226\214/note-2021-07-14(\351\201\215\345\216\206\345\207\272\350\267\257\347\224\261\350\267\257\345\276\204).md" (100%) diff --git "a/\346\235\250\345\256\207\346\226\214/note-2021-07-15(\351\201\215\345\216\206\345\207\272\350\267\257\347\224\261\350\267\257\345\276\204).md" "b/\346\235\250\345\256\207\346\226\214/note-2021-07-14(\351\201\215\345\216\206\345\207\272\350\267\257\347\224\261\350\267\257\345\276\204).md" similarity index 100% rename from "\346\235\250\345\256\207\346\226\214/note-2021-07-15(\351\201\215\345\216\206\345\207\272\350\267\257\347\224\261\350\267\257\345\276\204).md" rename to "\346\235\250\345\256\207\346\226\214/note-2021-07-14(\351\201\215\345\216\206\345\207\272\350\267\257\347\224\261\350\267\257\345\276\204).md" -- Gitee From 6167f6b23812d388e0d52473740be1e0bd370992 Mon Sep 17 00:00:00 2001 From: yang <2385659228@qq.com> Date: Fri, 16 Jul 2021 16:44:09 +0800 Subject: [PATCH 30/33] =?UTF-8?q?ECharts=EF=BC=8Ciconfont=E6=B7=98?= =?UTF-8?q?=E5=AE=9D=E5=9B=BE=E6=A0=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\207,axios,\350\267\250\345\237\237,).md" | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 "\346\235\250\345\256\207\346\226\214/note-2021-07-16(ECharts\357\274\214iconfont\346\267\230\345\256\235\345\233\276\346\240\207,axios,\350\267\250\345\237\237,).md" diff --git "a/\346\235\250\345\256\207\346\226\214/note-2021-07-16(ECharts\357\274\214iconfont\346\267\230\345\256\235\345\233\276\346\240\207,axios,\350\267\250\345\237\237,).md" "b/\346\235\250\345\256\207\346\226\214/note-2021-07-16(ECharts\357\274\214iconfont\346\267\230\345\256\235\345\233\276\346\240\207,axios,\350\267\250\345\237\237,).md" new file mode 100644 index 0000000..9d20b55 --- /dev/null +++ "b/\346\235\250\345\256\207\346\226\214/note-2021-07-16(ECharts\357\274\214iconfont\346\267\230\345\256\235\345\233\276\346\240\207,axios,\350\267\250\345\237\237,).md" @@ -0,0 +1,61 @@ +# ECharts,iconfont淘宝图标,axios,跨域, +## ECharts +1. 安装 ECharts +``` +npm install echarts --save +yarn add echarts +``` +2. 引入 ECharts main.js +``` +import * as echarts from 'echarts'; +import 'echarts-gl'; + +Vue.use(ElementUI); +Vue.prototype.$echarts = echarts +``` +3. Diqiu.vue +``` + + + +``` +2. 加入通用css代码(引入一次就行) 创建icon.css src/assets/icon.css +``` +.icon { + width: 1em; height: 1em; + vertical-align: -0.15em; + fill: currentColor; + overflow: hidden; + } +``` +3. 注册使用 +``` +import '../src/assets/icon.css' +``` +4. 挑选相应图标并获取类名,应用于页面 +``` + +``` +## axios +1. -- Gitee From d810ef2a966b45cf7610e826fbcf10f53cffe4a4 Mon Sep 17 00:00:00 2001 From: yang <2385659228@qq.com> Date: Fri, 16 Jul 2021 17:19:29 +0800 Subject: [PATCH 31/33] =?UTF-8?q?ECharts=EF=BC=8Ciconfont=E6=B7=98?= =?UTF-8?q?=E5=AE=9D=E5=9B=BE=E6=A0=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...233\276\346\240\207,axios,\350\267\250\345\237\237,).md" | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git "a/\346\235\250\345\256\207\346\226\214/note-2021-07-16(ECharts\357\274\214iconfont\346\267\230\345\256\235\345\233\276\346\240\207,axios,\350\267\250\345\237\237,).md" "b/\346\235\250\345\256\207\346\226\214/note-2021-07-16(ECharts\357\274\214iconfont\346\267\230\345\256\235\345\233\276\346\240\207,axios,\350\267\250\345\237\237,).md" index 9d20b55..dad3145 100644 --- "a/\346\235\250\345\256\207\346\226\214/note-2021-07-16(ECharts\357\274\214iconfont\346\267\230\345\256\235\345\233\276\346\240\207,axios,\350\267\250\345\237\237,).md" +++ "b/\346\235\250\345\256\207\346\226\214/note-2021-07-16(ECharts\357\274\214iconfont\346\267\230\345\256\235\345\233\276\346\240\207,axios,\350\267\250\345\237\237,).md" @@ -58,4 +58,8 @@ import '../src/assets/icon.css' ``` ## axios -1. +1.安装axios +``` + npm install axios + yarn add axios +``` -- Gitee From c0a50803eab4ee3039b780e7ba07c77dbe96c115 Mon Sep 17 00:00:00 2001 From: yang <2385659228@qq.com> Date: Sat, 17 Jul 2021 09:05:28 +0800 Subject: [PATCH 32/33] =?UTF-8?q?ECharts=EF=BC=8Ciconfont=E6=B7=98?= =?UTF-8?q?=E5=AE=9D=E5=9B=BE=E6=A0=87,axios,=E8=B7=A8=E5=9F=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\207,axios,\350\267\250\345\237\237,).md" | 63 ++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git "a/\346\235\250\345\256\207\346\226\214/note-2021-07-16(ECharts\357\274\214iconfont\346\267\230\345\256\235\345\233\276\346\240\207,axios,\350\267\250\345\237\237,).md" "b/\346\235\250\345\256\207\346\226\214/note-2021-07-16(ECharts\357\274\214iconfont\346\267\230\345\256\235\345\233\276\346\240\207,axios,\350\267\250\345\237\237,).md" index dad3145..fd81583 100644 --- "a/\346\235\250\345\256\207\346\226\214/note-2021-07-16(ECharts\357\274\214iconfont\346\267\230\345\256\235\345\233\276\346\240\207,axios,\350\267\250\345\237\237,).md" +++ "b/\346\235\250\345\256\207\346\226\214/note-2021-07-16(ECharts\357\274\214iconfont\346\267\230\345\256\235\345\233\276\346\240\207,axios,\350\267\250\345\237\237,).md" @@ -58,8 +58,69 @@ import '../src/assets/icon.css' ``` ## axios -1.安装axios +1. 安装axios ``` npm install axios yarn add axios ``` +2. 创建实例 src/utils/request.js +``` +import axios from 'axios' +const instance = axios.create({ + baseURL: 'http://localhost:5000/', + timeout: 1000, +}); +//暴露模块 +export default instance +``` +3. 定义方法获取指定id的值 src/api/users.js +``` +import request from '../utils/request' + +const getUserById=(id)=>{ + return request.get(`/users/${id}`) +} +export default getUserById +``` +4. 使用在前端 +``` + +``` +## 跨域 +1. 设置允许所有来源跨域,在StartUp类的ConfigureServices方法中添加如下代码: +``` + services.AddCors(Options=>{ + Options.AddPolicy(allowCors,builder => + { + builder.AllowAnyHeader() + .AllowAnyMethod() + .AllowAnyOrigin(); + }); + }); +``` +2. 修改Configure方法 +``` + // 跨域 允许所有跨域,allowCors是在ConfigureServices方法中配置的跨域策略名称 + app.UseCors(allowCors); +``` +3. 设置特定来源可以跨域,修改ConfigureServices方法代码如下: +``` +//允许一个或多个来源可以跨域 + services.AddCors(Options=>{ + Options.AddPolicy(allowCors,builder => + { + // 设定允许跨域的来源 + builder.AllowAnyHeader() + .AllowAnyMethod() + .AllowAnyOrigin(); + }); + }); +``` + -- Gitee From 5654addc2f9617144f189fe9e32c68c5de7cf802 Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 20 Jul 2021 10:36:51 +0800 Subject: [PATCH 33/33] =?UTF-8?q?=E5=88=86=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\346\210\267\345\210\206\351\241\265).md" | 152 ++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 "\346\235\250\345\256\207\346\226\214/note-2021-07-17(\347\224\250\346\210\267\345\210\206\351\241\265).md" diff --git "a/\346\235\250\345\256\207\346\226\214/note-2021-07-17(\347\224\250\346\210\267\345\210\206\351\241\265).md" "b/\346\235\250\345\256\207\346\226\214/note-2021-07-17(\347\224\250\346\210\267\345\210\206\351\241\265).md" new file mode 100644 index 0000000..e89dfcf --- /dev/null +++ "b/\346\235\250\345\256\207\346\226\214/note-2021-07-17(\347\224\250\346\210\267\345\210\206\351\241\265).md" @@ -0,0 +1,152 @@ +# 分页 +1. vue cli Users.vue +``` + + + +``` +2. web Api UsersController +``` + public dynamic Get([FromQuery] Pager pager) + { + // var pageIndex=pager.PageIndex; + // var pageSize=pager.PageSize; + var pageIndex=int.Parse(Request.Query["pageIndex"][0]); + System.Console.WriteLine(pageIndex); + //获取前端vue定义的pageIndex放入一个数组里获取第一个 + var pageSize=int.Parse(Request.Query["pageSize"][0]); + + var user = _usersRespository.Table; + var dt=user.Skip((pageIndex-1)*pageSize).Take(pageSize).ToList(); + //Skip()方法先忽略根据页面的大小和实际的页数计算出的项数,再使用方法Take()根据页面的大小提取一定数量的项: + var res= new + { + Code = 1000, + Data = new {Data =dt,Pager =new {pageIndex,pageSize,rowsTotal=user.Count()}}, + Msg = "获取用户列表成功^_^" + }; + return res; + } +``` +3. 创建类定义字段 ParamModel/Pager.cs +``` +namespace MyApi.Api.ParamModel +{ + public class Pager + { + // 当前页面 + public int PageIndex{get;set;} + + // 当前页条数 + public int PageSize{get;set;} + + // 总条数 + public int RowsTotal{get;set;} + } +} +``` \ No newline at end of file -- Gitee