diff --git "a/\345\210\230\346\263\242/2021-6-22.md" "b/\345\210\230\346\263\242/2021-6-22.md" new file mode 100644 index 0000000000000000000000000000000000000000..426ad9b45c1427315382b81f1c83435d6b903959 --- /dev/null +++ "b/\345\210\230\346\263\242/2021-6-22.md" @@ -0,0 +1,6 @@ +# webAPI第一天 +* dotnet new sln 创建解决方案 +* dotnet new webapi -n MyApp.Api --no-https 创建webapi +* dotnet run运行项目 +* 将项目添加至解决方案dotnet sln add Project.Api +* 运行项目(不在Project.Api中)dotnet run -p Project.Api diff --git "a/\345\210\230\346\263\242/2021-6-23.md" "b/\345\210\230\346\263\242/2021-6-23.md" new file mode 100644 index 0000000000000000000000000000000000000000..c9c1d820d73decab07881bfda8a5a632d35f3ee1 --- /dev/null +++ "b/\345\210\230\346\263\242/2021-6-23.md" @@ -0,0 +1,87 @@ +# WebAPI第二天 +* 在扩展中安装REST Client来测试路由 +* Prettier - Code foematter插件可以格式化C#语言 +* 在C#中设置路由以及访问类型然后通过Rest client测试 +* 前端传值对后端进行修改 +``` +namespace MyApp.Api.model +{ + public class Animal + { + public int Id {get;set;} + public string Type {get;set;} + public string Name {get;set;} + } +} +``` +``` +public class ZooController:ControllerBase + { + [HttpGet("{id}")] + //获取一个动物数据 + public dynamic GetOneAnimal(int id){ + var animals = GetAllAnimal(); + var animal = animals.Where(x=>x.Id==id).FirstOrDefault(); + return animal; + } + + [HttpPost] + public dynamic CreateAnimal(dynamic model){ + return new { + code = 200, + Data = model, + msg = "ojbk" + }; + } + + [HttpPut("{id}")] + public dynamic Update(int id,dynamic model){ + return new { + code =200, + Data = model, + msg = string.Format("更改的用户Id为:{0}",id) + }; + } + + //初始化一个动物园列表 + public IEnumerable GetAllAnimal(){ + var animal = new List{ + new Animal{ + Id=1, + Type="猴子", + Name="少黄" + },new Animal{ + Id=2, + Type="猩猩", + Name="少皇" + } + }; + + return animal; + } + + } +} +``` +``` +### +GET http://localhost:5000/zoo/2 HTTP/1.1 + +### +POST http://localhost:5000/zoo HTTP/1.1 +Content-Type: application/json + +{ + "Type":"大猴子", + "Name":"大少黄" +} + +### +PUT http://localhost:5000/zoo/2 HTTP/1.1 +Content-Type: application/json + +{ + "Type":"灵明石猴", + "Name":"大少黄" +} +``` \ No newline at end of file diff --git "a/\345\210\230\346\263\242/2021-6-26.md" "b/\345\210\230\346\263\242/2021-6-26.md" new file mode 100644 index 0000000000000000000000000000000000000000..8f3416a386a368c5710bbd5d30fc075f30796849 --- /dev/null +++ "b/\345\210\230\346\263\242/2021-6-26.md" @@ -0,0 +1,141 @@ +# WebApi第三天 +* dotnet new sln 创建解决方案 +* dotnet new webapi -n MyApp.Api --no-https 创建webapi +* dotnet add package Microsoft.EntityFrameworkCore.SqlServer命令在项目中安装数据库 +* 创建model文件夹在其中创建数据库表模型 +``` + 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 class ZooDb : DbContext + { + public DbSet Users {get;set;} + + public DbSet Animals {get;set;} + + protected override void OnConfiguring(DbContextOptionsBuilder options) + =>options.UseSqlServer(@"server=.;database=Zoo;uid = Lioubo;pwd = 123456lb;"); + } +``` +* 安装dotnet ef和设计包 +* dotnet tool install --global dotnet-ef +* dotnet add package Microsoft.EntityFrameworkCore。Design +* 迁移创建数据库基架 +* dotnet ef migrations add InitialCreate +* 最后 dotnet ef database update创建数据库 +* 数据库的增删改查在控制器的中使用 +``` +[ApiController] + [Route("[controller]")] + public class ZooController : ControllerBase + { + private ZooDb zoo = new ZooDb(); + + + //添加用户数据 + [HttpPost] + public string AddUser(UserParam model) + { + var user = new User + { + Username = model.Username, + Password = model.Password + }; + + zoo.Users.Add(user); + zoo.SaveChanges(); + + var res = new + { + Code = 1000, + Data = user, + Msg = "ojbk" + }; + + return JsonConvert.SerializeObject(res); + + + } + + //删除用户数据 + [HttpDelete("{id}")] + public dynamic DeleteUser(int id) + { + + var use = zoo.Users.Where(x => x.Id == id).FirstOrDefault(); + + if (use != null) + { + + zoo.Users.Remove(use); + zoo.SaveChanges(); + return "删除成功"; + + } + else + { + return "该用户不存在"; + } + + } + + //更新用户数据 + [HttpPut("{id}")] + public dynamic UpdateUser(int id, UserParam model) + { + + var use = zoo.Users.Where(x => x.Id == id).FirstOrDefault(); + + if (use != null) + { + use.Username = model.Username; + use.Password = model.Password; + use.UpdateTime = DateTime.Now; + + zoo.Users.Update(use); + zoo.SaveChanges(); + + return "更新成功"; + } + else + { + return "该用户不存在"; + } + + } + + //查找用户数据 + [HttpGet("{id}")] + public dynamic SelectUser(int id) + { + var use = zoo.Users.Where(x => x.Id == id).FirstOrDefault(); + if (use != null) + { + return new + { + Id = use.Id, + Username=use.Username, + Password = use.Password + }; + } + else + { + return "该用户不存在"; + } + } + } +``` \ No newline at end of file diff --git "a/\345\210\230\346\263\242/2021-6-29.md" "b/\345\210\230\346\263\242/2021-6-29.md" new file mode 100644 index 0000000000000000000000000000000000000000..fa4de0401df81a8a94b717ae318a7028dddba286 --- /dev/null +++ "b/\345\210\230\346\263\242/2021-6-29.md" @@ -0,0 +1,93 @@ +# WebApi的第四天 +* dotnet restore可以还原项目 +* 在表中设置外键 +``` +public class UserRoles: Base + { + public int UserId {get;set;} + + public int RolesId {get;set;} + + public virtual User User{get;set;} + + public virtual Roles Roles {get;set;} + } +``` +``` +public class Roles:Base + { + public string RoleName {get;set;} + + public virtual IEnumerable UserRoles { get; set; } + } + + +public class User:Base + { + public string Username { get; set; } + + public string Password { get; set; } + + public virtual IEnumerable UserRoles { get; set; } + } +``` +* 定义接口 +``` +public interface IRepository + { + IQueryable Table{get;set;} + + //根据id获取数据 + T GetById(int id); + + /// 使用实体对象,插入数据 + void Insert(T entity); + + /// 使用实体对象,插入数据(异步) + Task InserAsync(T entity); + /// 根据对象更新数据 + void Update(T entity); + /// 根据对象更新数据(异步) + Task UpdatedAsync(T entity); + ///利用实体删除对象 + void Delete(int id); + + } +``` +* 实现接口 +``` +public class EfRepository : IRepository + { + public IQueryable Table { get => throw new System.NotImplementedException(); set => throw new System.NotImplementedException(); } + + public void Delete(int id) + { + throw new System.NotImplementedException(); + } + + public T GetById(int id) + { + throw new System.NotImplementedException(); + } + + public Task InserAsync(T entity) + { + throw new System.NotImplementedException(); + } + + public void Insert(T entity) + { + throw new System.NotImplementedException(); + } + + public void Update(T entity) + { + throw new System.NotImplementedException(); + } + + public Task UpdatedAsync(T entity) + { + throw new System.NotImplementedException(); + } + } +``` \ No newline at end of file diff --git "a/\345\210\230\346\263\242/2021-6-30.md" "b/\345\210\230\346\263\242/2021-6-30.md" new file mode 100644 index 0000000000000000000000000000000000000000..e7c01313114b97107abf74ddeb0c7a431758337b --- /dev/null +++ "b/\345\210\230\346\263\242/2021-6-30.md" @@ -0,0 +1,95 @@ +# WebApi的第五天 +* 在继承接口的类中实现的反法定义 +``` +public class EfRepository : IRepository where T : Base + { + + //数据库上下文实例对象 + private UserDb db = new UserDb(); + + //字段成员用于Entity属性 + private DbSet _entity; + + protected DbSet Entity + { + get{ + if(_entity ==null){ + _entity = db.Set(); + } + return _entity; + } + } + + public IQueryable Table + { + get + { + return Entity.AsQueryable(); + } + } + + //删除T类型数据库中指定的表 + public void Delete(int id) + { + var t = Entity.Where(x=>x.Id ==id).FirstOrDefault(); + Entity.Remove(t); + db.SaveChanges(); + } + + //查找T表中的id + public T GetById(int id) + { + var t = Entity.Where(x=>x.Id==id).FirstOrDefault(); + return t; + } + + + public async Task InserAsync(T entity) + { + if(entity == null){ + throw new System.NullReferenceException(); + } + + entity.IsActivited = true; + entity.IsDelected = false; + entity.CreatedTime = DateTime.Now; + entity.UpdatedTime = DateTime.Now; + + await Entity.AddAsync(entity); + await db.SaveChangesAsync(); + } + + public void Insert(T entity) + { + if(entity == null){ + throw new System.NullReferenceException(); + } + + entity.IsActivited = true; + entity.IsDelected = false; + entity.CreatedTime = DateTime.Now; + entity.UpdatedTime = DateTime.Now; + + Entity.Add(entity); + db.SaveChanges(); + } + + public void Update(T entity) + { + if(entity == null){ + throw new System.NullReferenceException(); + } + entity.UpdatedTime = DateTime.Now; + db.SaveChanges(); + } + + public async Task UpdatedAsync(T entity) + { + if(entity == null){ + throw new System.NullReferenceException(); + } + entity.UpdatedTime = DateTime.Now; + await db.SaveChangesAsync(); + } + } +``` \ No newline at end of file diff --git "a/\345\210\230\346\263\242/2021-7-10.md" "b/\345\210\230\346\263\242/2021-7-10.md" new file mode 100644 index 0000000000000000000000000000000000000000..b4d897368ca3e5dc74f58963c40d62619cc5e41a --- /dev/null +++ "b/\345\210\230\346\263\242/2021-7-10.md" @@ -0,0 +1,46 @@ +# Vue +* 包装路由 +``` +import Layout from "../components/Layout" + +let routes = [ + { + path:"/", + component:Layout, + children:[ + { + path:"/Zoo", + component:()=>import("../components/Zoo") + } + ] + } +] + +export default routes +``` +``` +import Vue from 'vue' +import VueRoute from 'vue-router' +import routes from './routes' + +Vue.use(VueRoute); + +let router = new VueRoute({ + mode:'history', + routes +}) + +export default router +``` +``` +import Vue from 'vue' +import App from './App.vue' +import router from './router/index' + + +new Vue({ + router, + render: h => h(App), +}).$mount('#app') + +``` \ No newline at end of file diff --git "a/\345\210\230\346\263\242/2021-7-3.md" "b/\345\210\230\346\263\242/2021-7-3.md" new file mode 100644 index 0000000000000000000000000000000000000000..3bdef5b129d03c075080382bc99f0577438e3b1f --- /dev/null +++ "b/\345\210\230\346\263\242/2021-7-3.md" @@ -0,0 +1,34 @@ +# C#基础复习 +## 接口 +1. 什么是接口 +``` + + 定义同一类型的行为,接口中包含方法但是没有实现,继承了接口要实现它的所有方法与代码, + +``` +2. 接口的用法 +``` + + 用类去继承接口,一个类可以继承多个接口 + +``` +## 泛型 +1. 什么是泛型 +``` + +是一种数据类型,可以用来接收任意数据类型,或者用where添加约束指定某种类型 + +``` +2. 泛型的用法 +``` + +1.泛型集合 +2.泛型类型 +3.泛型接口 + +``` +## 类 +1. 要处于命名空间下 +2. 类型的成员(属性(具有get,set的类型成员),方法,事件,字段(类型中的变量)) +3. 自动属性具有get,set的类型成员但是没有具体定义 + diff --git "a/\345\210\230\346\263\242/2021-7-6.md" "b/\345\210\230\346\263\242/2021-7-6.md" new file mode 100644 index 0000000000000000000000000000000000000000..7b1eadebed85dcb7a7378ee5a1a791e529105a06 --- /dev/null +++ "b/\345\210\230\346\263\242/2021-7-6.md" @@ -0,0 +1,62 @@ +# JWT +* jwt用json类型来保证数据的安全 +* dotnet add package Microsoft.AspNetCore.Authentication.JwtBearer安装JWt +* appsetting.json中配置token +``` +"TokenParameter": { + "Secret": "123456123456123456", + "Issuer": "帅比", + "AccessExpiration": 120, + "RefreshExpiration": 1440 + } +``` +* 创建token的模板获取配置里的数据 +``` +public class TokenParamter + { + //token密钥 + public string Secret {get;set;} + //发行人 + public string Issuer {get;set;} + //token作用时间 + public int AccessExpiration {get;set;} + //类似作用时间,超时用户要重新登录 + public int RefreshExpiration {get;set;} + } +``` +``` + private IRepository _usersRespository; + private TokenParamter _tokenparamter; + private readonly IConfiguration _configuration; + + public UserController(IRepository usersRespository,IConfiguration configuration) + { + _configuration = configuration; + _usersRespository = usersRespository; + //获取配置的token数据 + _tokenparamter = _configuration.GetSection("TokenParameter").Get(); + } +``` +* 生成token的代码包装方法 +``` +public class TokenHelper + { + public static string GenerateToekn(TokenParamter tokenParameter,User user) + { + + var claims = new[] + { + new Claim(ClaimTypes.Name, user.Username), + new Claim(ClaimTypes.Role, "admin"), + }; + + 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; + } + } +``` \ No newline at end of file diff --git "a/\345\210\230\346\263\242/2021-7-8.md" "b/\345\210\230\346\263\242/2021-7-8.md" new file mode 100644 index 0000000000000000000000000000000000000000..3537ab0b01d81945759d2d6780e38b31c690f138 --- /dev/null +++ "b/\345\210\230\346\263\242/2021-7-8.md" @@ -0,0 +1,42 @@ +# token认证 +* 在startup的ConfigureServices中添加验证器 +``` +// 使用配置中间件,获得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(options => + { + options.Filters.Add(typeof(AuditLogActionFilter)); + }); +``` +* Configure方法中添加中间件 +``` +// 将token的验证注册到中间件 + app.UseAuthentication(); + app.UseAuthorization(); +``` +* 在路由上设置认证 +``` + [Authorize]//设置token认证 +``` +* 设置匿名访问 +``` +[AllowAnonymous] +``` +* 通过方法获取token,使用token认证使用方法 +``` +### +GET http://localhost:5000/user HTTP/1.1 +Authorization: Bearer (token) +``` \ No newline at end of file diff --git "a/\345\210\230\346\263\242/photo/2021-6-19 14-34-54.JPG" "b/\345\210\230\346\263\242/photo/2021-6-19 14-34-54.JPG" deleted file mode 100644 index 5008f48c761e3bcc4ef85f8536e82da1130c4b2e..0000000000000000000000000000000000000000 Binary files "a/\345\210\230\346\263\242/photo/2021-6-19 14-34-54.JPG" and /dev/null differ diff --git "a/\345\210\230\346\263\242/photo/2021-6-19 15-12-17.JPG" "b/\345\210\230\346\263\242/photo/2021-6-19 15-12-17.JPG" deleted file mode 100644 index d6b549231312a3b17c43b533d1cdf5c8c04277d7..0000000000000000000000000000000000000000 Binary files "a/\345\210\230\346\263\242/photo/2021-6-19 15-12-17.JPG" and /dev/null differ diff --git "a/\345\210\230\346\263\242/photo/2021-6-19 15-25-6.JPG" "b/\345\210\230\346\263\242/photo/2021-6-19 15-25-6.JPG" deleted file mode 100644 index 0419929bd941b21417f8e86fc6f010075a9b25e6..0000000000000000000000000000000000000000 Binary files "a/\345\210\230\346\263\242/photo/2021-6-19 15-25-6.JPG" and /dev/null differ diff --git "a/\345\210\230\346\263\242/photo/2021-6-19 15-35-48.JPG" "b/\345\210\230\346\263\242/photo/2021-6-19 15-35-48.JPG" deleted file mode 100644 index 53126d5d7e9d5ab74bc8925ed4ff333b4f55a11a..0000000000000000000000000000000000000000 Binary files "a/\345\210\230\346\263\242/photo/2021-6-19 15-35-48.JPG" and /dev/null differ