v3.0.1
v3.0.1
This commit is contained in:
@@ -8,7 +8,7 @@
|
||||
<PackageReference Include="Autofac" Version="6.1.0" />
|
||||
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="7.1.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.5" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.4">
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.6">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
|
||||
@@ -29,14 +29,12 @@ namespace CC.Yi.API.Controllers
|
||||
|
||||
//private UserManager<result_user> _userManager;//处理用户相关逻辑:添加密码,修改密码,添加删除角色等等
|
||||
//private SignInManager<result_user> _signInManager;//处理注册登录的相关逻辑
|
||||
private IpropBll _propBll;
|
||||
private IstudentBll _studentBll;
|
||||
public StudentController(ILogger<StudentController> logger, IstudentBll studentBll,IpropBll propBll)
|
||||
public StudentController(ILogger<StudentController> logger, IstudentBll studentBll)
|
||||
{
|
||||
_logger = logger;
|
||||
_logger.LogInformation("现在你进入了StudentController控制器");
|
||||
_logger.LogInformation("现在你进入了StudentController控制器");//nlog日志模块
|
||||
_studentBll = studentBll;
|
||||
_propBll = propBll;
|
||||
}
|
||||
#region
|
||||
//关于身份认证配置使用:
|
||||
@@ -56,42 +54,20 @@ namespace CC.Yi.API.Controllers
|
||||
// return Ok();
|
||||
//}
|
||||
|
||||
[HttpGet]
|
||||
public Result test0()
|
||||
{
|
||||
|
||||
_propBll.Add(new prop { name = "666" });
|
||||
|
||||
return Result.Success();
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public Result test1()
|
||||
{
|
||||
var data = _studentBll.GetEntities(u => u.id == 1).Include(u=>u.props).FirstOrDefault();
|
||||
var myp = _propBll.GetEntities(u => u.id == 1).FirstOrDefault();
|
||||
data.props.Add(myp);
|
||||
data.props.Add(myp);
|
||||
_studentBll.Update(data);
|
||||
return Result.Success().SetData(new {data.id, data.name,props=
|
||||
data.props.Select(u=>new {
|
||||
u.id,
|
||||
u.name
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
#region
|
||||
//redis操作
|
||||
#endregion
|
||||
[HttpGet]
|
||||
public Result GetReids()
|
||||
{
|
||||
var data = CacheHelper.CacheWriter.GetCache<string>("key01");
|
||||
return Result.Success(data);
|
||||
}
|
||||
|
||||
|
||||
#region
|
||||
//下面,权限验证
|
||||
#endregion
|
||||
|
||||
//发送令牌
|
||||
[HttpGet]
|
||||
public Result Login(string role)
|
||||
{
|
||||
@@ -116,7 +92,7 @@ namespace CC.Yi.API.Controllers
|
||||
|
||||
var tokenData = new JwtSecurityTokenHandler().WriteToken(token);
|
||||
return Result.Success("欢迎你!管理员!").SetData(new { token = tokenData });
|
||||
}
|
||||
}//发送令牌
|
||||
|
||||
[HttpGet]
|
||||
[Authorize(Policy = "myadmin")]//基于策略的验证
|
||||
@@ -140,49 +116,32 @@ namespace CC.Yi.API.Controllers
|
||||
public async Task<Result> GetTest()//查
|
||||
{
|
||||
_logger.LogInformation("调用查方法");
|
||||
var data =await _studentBll.GetAllEntities().ToListAsync();
|
||||
var data = await _studentBll.GetAllEntities().ToListAsync();
|
||||
return Result.Success().SetData(data);
|
||||
}
|
||||
[HttpGet]
|
||||
public Result AddTest()//增
|
||||
{
|
||||
_logger.LogInformation("调用增方法");
|
||||
List<student> students = new List<student>() { new student { name = "学生a" }, new student { name = "学生d" } };
|
||||
if (_studentBll.Add(students))
|
||||
{
|
||||
return Result.Success();
|
||||
}
|
||||
else
|
||||
{
|
||||
return Result.Error();
|
||||
}
|
||||
List<student> students = new List<student>() { new student { name = "学生a" } };
|
||||
_studentBll.Add(students);
|
||||
return Result.Success();
|
||||
|
||||
}
|
||||
[HttpGet]
|
||||
public Result RemoveTest()//删
|
||||
{
|
||||
_logger.LogInformation("调用删方法");
|
||||
if (_studentBll.Delete(u => u.name == "学生a"))
|
||||
{
|
||||
return Result.Success();
|
||||
}
|
||||
else
|
||||
{
|
||||
return Result.Error();
|
||||
}
|
||||
_studentBll.Delete(u => u.id==1);
|
||||
return Result.Success();
|
||||
|
||||
}
|
||||
[HttpGet]
|
||||
public Result UpdateTest()//改
|
||||
public Result UpdateTest()//改
|
||||
{
|
||||
_logger.LogInformation("调用改方法");
|
||||
if (_studentBll.Update(new student { id = 2, name = "学生a" }, "name"))
|
||||
{
|
||||
return Result.Success();
|
||||
}
|
||||
else
|
||||
{
|
||||
return Result.Error();
|
||||
}
|
||||
|
||||
_studentBll.Update(new student { id = 1, name = "学生b" }, "name");
|
||||
return Result.Success();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
72
CC.Yi/CC.Yi.API/Extension/ErrorHandExtension.cs
Normal file
72
CC.Yi/CC.Yi.API/Extension/ErrorHandExtension.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
using CC.Yi.Common;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CC.Yi.API.Extension
|
||||
{
|
||||
public class ErrorHandExtension
|
||||
{
|
||||
private readonly RequestDelegate next;
|
||||
|
||||
public ErrorHandExtension(RequestDelegate next)
|
||||
{
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
public async Task Invoke(HttpContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
await next(context);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var statusCode = context.Response.StatusCode;
|
||||
if (ex is ArgumentException)
|
||||
{
|
||||
statusCode = 200;
|
||||
}
|
||||
await HandleExceptionAsync(context, statusCode, ex.Message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
var statusCode = context.Response.StatusCode;
|
||||
var msg = "";
|
||||
|
||||
switch (statusCode)
|
||||
{
|
||||
|
||||
case 401: msg = "未授权";break;
|
||||
case 403: msg = "未授权"; break;
|
||||
case 404: msg = "未找到服务"; break;
|
||||
case 502: msg = "请求错误"; break;
|
||||
|
||||
}
|
||||
if (!string.IsNullOrWhiteSpace(msg))
|
||||
{
|
||||
await HandleExceptionAsync(context, statusCode, msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
//异常错误信息捕获,将错误信息用Json方式返回
|
||||
private static Task HandleExceptionAsync(HttpContext context, int statusCode, string msg)
|
||||
{
|
||||
var result = JsonConvert.SerializeObject( Result.Error(msg).SetCode(statusCode));
|
||||
context.Response.ContentType = "application/json;charset=utf-8";
|
||||
return context.Response.WriteAsync(result);
|
||||
}
|
||||
}
|
||||
//扩展方法
|
||||
public static class ErrorHandlingExtensions
|
||||
{
|
||||
public static IApplicationBuilder UseErrorHandling(this IApplicationBuilder builder)
|
||||
{
|
||||
return builder.UseMiddleware<ErrorHandExtension>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -69,7 +69,7 @@ namespace CC.Yi.API.Extension
|
||||
//在 Startup.Configure 方法中,启用中间件为生成的 JSON 文档和 Swagger UI 提供服务:
|
||||
// Enable middleware to serve generated Swagger as a JSON endpoint.
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "JwtTest v1"));
|
||||
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Yi"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
71
CC.Yi/CC.Yi.API/Init/InitDb.cs
Normal file
71
CC.Yi/CC.Yi.API/Init/InitDb.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using CC.Yi.Model;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CC.Yi.API.Init
|
||||
{
|
||||
public static class InitDb
|
||||
{
|
||||
public static bool Init(DataContext Db)
|
||||
{
|
||||
//if (!Db.Set<user>().Any())
|
||||
//{
|
||||
// user initUser = new user
|
||||
// {
|
||||
// username = "cc",
|
||||
// password = "123",
|
||||
// user_extra = new user_extra(),
|
||||
// time = DateTime.Now,
|
||||
// roles = new List<role>{
|
||||
// new role{
|
||||
// role_name="管理员",
|
||||
// actions=new List<action>{
|
||||
// new action{ action_name="首页",router="/index",icon="mdi-view-dashboard"},
|
||||
// new action{action_name="用户管理",router="/user",icon="mdi-account-box"},
|
||||
// new action{ action_name="角色管理",router="/role",icon="mdi-gavel"},
|
||||
// new action{ action_name="权限管理",router="/action",icon="mdi-lock"}
|
||||
// }
|
||||
// },
|
||||
// new role{ role_name="l-1"},
|
||||
// new role{ role_name="l-2"},
|
||||
// new role{ role_name="l-3"},
|
||||
// new role{ role_name="l-4"},
|
||||
// new role{ role_name="l-5"},
|
||||
// new role{ role_name="l-6"},
|
||||
// new role{ role_name="l-7"},
|
||||
// new role{ role_name="l-8"},
|
||||
// new role{ role_name="l-9"},
|
||||
// new role{ role_name="l-10"},
|
||||
// new role{ role_name="l-11"},
|
||||
// new role{ role_name="l-12"},
|
||||
// new role{ role_name="普通用户"}
|
||||
// }
|
||||
// };
|
||||
// Db.Set<user>().Update(initUser);
|
||||
// //-------------------------------------------------------------------------------------添加管理员账户
|
||||
// List<level> levels = new List<level>
|
||||
// {
|
||||
// new level{num=0, name="小白0",max_char=50,experience=0},
|
||||
// new level{num=1, name="小白1",max_char=100,experience=5},
|
||||
// new level{num=2, name="小白2",max_char=200,experience=10},
|
||||
// new level{num=3, name="小白3",max_char=300,experience=15},
|
||||
// new level{num=4, name="小白4",max_char=400,experience=20},
|
||||
// new level{num=5, name="小白5",max_char=500,experience=25},
|
||||
// new level{num=6, name="小白6",max_char=600,experience=30},
|
||||
// new level{num=7, name="小白7",max_char=700,experience=35},
|
||||
// new level{num=8, name="小白8",max_char=800,experience=40},
|
||||
// new level{num=9, name="小白9",max_char=900,experience=45},
|
||||
// new level{num=10, name="小白10",max_char=1000,experience=50},
|
||||
// new level{num=11, name="小白11",max_char=1100,experience=55}
|
||||
// };
|
||||
// Db.Set<level>().AddRange(levels);
|
||||
// //---------------------------------------------------------------------------------------添加等级表
|
||||
|
||||
// return Db.SaveChanges()>0;
|
||||
//}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
// <auto-generated />
|
||||
using CC.Yi.Model;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
namespace CC.Yi.API.Migrations
|
||||
{
|
||||
[DbContext(typeof(DataContext))]
|
||||
[Migration("20210413063257_y1")]
|
||||
partial class y1
|
||||
{
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "5.0.5");
|
||||
|
||||
modelBuilder.Entity("CC.Yi.Model.student", b =>
|
||||
{
|
||||
b.Property<int>("id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("name")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.HasKey("id");
|
||||
|
||||
b.ToTable("student");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
namespace CC.Yi.API.Migrations
|
||||
{
|
||||
public partial class y1 : Migration
|
||||
{
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "student",
|
||||
columns: table => new
|
||||
{
|
||||
id = table.Column<int>(type: "INTEGER", nullable: false)
|
||||
.Annotation("Sqlite:Autoincrement", true),
|
||||
name = table.Column<string>(type: "TEXT", nullable: true)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_student", x => x.id);
|
||||
});
|
||||
}
|
||||
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "student");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using CC.Yi.Model;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
namespace CC.Yi.API.Migrations
|
||||
{
|
||||
[DbContext(typeof(DataContext))]
|
||||
[Migration("20210526080428_yi2")]
|
||||
partial class yi2
|
||||
{
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "5.0.5");
|
||||
|
||||
modelBuilder.Entity("CC.Yi.Model.prop", b =>
|
||||
{
|
||||
b.Property<int>("id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("name")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<int?>("studentid")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.HasKey("id");
|
||||
|
||||
b.HasIndex("studentid");
|
||||
|
||||
b.ToTable("prop");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("CC.Yi.Model.student", b =>
|
||||
{
|
||||
b.Property<int>("id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("name")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.HasKey("id");
|
||||
|
||||
b.ToTable("student");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("CC.Yi.Model.prop", b =>
|
||||
{
|
||||
b.HasOne("CC.Yi.Model.student", "student")
|
||||
.WithMany("props")
|
||||
.HasForeignKey("studentid");
|
||||
|
||||
b.Navigation("student");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("CC.Yi.Model.student", b =>
|
||||
{
|
||||
b.Navigation("props");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
namespace CC.Yi.API.Migrations
|
||||
{
|
||||
public partial class yi2 : Migration
|
||||
{
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "prop",
|
||||
columns: table => new
|
||||
{
|
||||
id = table.Column<int>(type: "INTEGER", nullable: false)
|
||||
.Annotation("Sqlite:Autoincrement", true),
|
||||
name = table.Column<string>(type: "TEXT", nullable: true),
|
||||
studentid = table.Column<int>(type: "INTEGER", nullable: true)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_prop", x => x.id);
|
||||
table.ForeignKey(
|
||||
name: "FK_prop_student_studentid",
|
||||
column: x => x.studentid,
|
||||
principalTable: "student",
|
||||
principalColumn: "id",
|
||||
onDelete: ReferentialAction.Restrict);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_prop_studentid",
|
||||
table: "prop",
|
||||
column: "studentid");
|
||||
}
|
||||
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "prop");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using CC.Yi.Model;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
namespace CC.Yi.API.Migrations
|
||||
{
|
||||
[DbContext(typeof(DataContext))]
|
||||
partial class DataContextModelSnapshot : ModelSnapshot
|
||||
{
|
||||
protected override void BuildModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "5.0.5");
|
||||
|
||||
modelBuilder.Entity("CC.Yi.Model.prop", b =>
|
||||
{
|
||||
b.Property<int>("id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("name")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<int?>("studentid")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.HasKey("id");
|
||||
|
||||
b.HasIndex("studentid");
|
||||
|
||||
b.ToTable("prop");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("CC.Yi.Model.student", b =>
|
||||
{
|
||||
b.Property<int>("id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("name")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.HasKey("id");
|
||||
|
||||
b.ToTable("student");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("CC.Yi.Model.prop", b =>
|
||||
{
|
||||
b.HasOne("CC.Yi.Model.student", "student")
|
||||
.WithMany("props")
|
||||
.HasForeignKey("studentid");
|
||||
|
||||
b.Navigation("student");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("CC.Yi.Model.student", b =>
|
||||
{
|
||||
b.Navigation("props");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,17 +17,16 @@ namespace CC.Yi.API
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>ݿ⣬<DDBF><E2A3AC><EFBFBD><EFBFBD><DEB8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
|
||||
//<2F><><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3><EFBFBD>࣬<EFBFBD><E0A3AC>ģ<EFBFBD>Ͳ<EFBFBD><CDB2>У<EFBFBD>ʹ<EFBFBD><CAB9>Add-Migration xxxǨ<78>ƣ<EFBFBD><C6A3><EFBFBD>ʹ<EFBFBD><CAB9>Update-Database<73><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD>
|
||||
//<2F><>T4Model<65><6C><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>T4
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>캯<EFBFBD><ECBAAF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD><D7A2>ֱ<EFBFBD><D6B1>ʹ<EFBFBD><CAB9>
|
||||
|
||||
var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
|
||||
try
|
||||
{
|
||||
logger.Debug("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Yi<59><69><EFBFBD><EFBFBD><EFBFBD>ܡ<EFBFBD><DCA1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
|
||||
var host = CreateHostBuilder(args).Build();
|
||||
//var scope = host.Services.CreateScope();
|
||||
//var services = scope.ServiceProvider;
|
||||
//var context = services.GetRequiredService<Model.DataContext>();//<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
|
||||
//DbContentFactory.Initialize(context);//<2F><><EFBFBD>þ<EFBFBD>̬<EFBFBD><EFBFBD><E0B7BD>ע<EFBFBD><D7A2>
|
||||
host.Run();
|
||||
logger.Info("Yi<59><69><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD><C9B9><EFBFBD>");
|
||||
}
|
||||
@@ -42,10 +41,6 @@ namespace CC.Yi.API
|
||||
// Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)
|
||||
NLog.LogManager.Shutdown();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||||
|
||||
@@ -3,6 +3,7 @@ using Autofac.Extras.DynamicProxy;
|
||||
using CC.Yi.API.Extension;
|
||||
using CC.Yi.BLL;
|
||||
using CC.Yi.Common.Castle;
|
||||
using CC.Yi.Common.Json;
|
||||
using CC.Yi.Common.Jwt;
|
||||
using CC.Yi.DAL;
|
||||
using CC.Yi.IBLL;
|
||||
@@ -11,6 +12,7 @@ using CC.Yi.Model;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.HttpsPolicy;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
@@ -44,60 +46,65 @@ namespace CC.Yi.API
|
||||
services.AddAuthorization(options =>
|
||||
{
|
||||
//<2F><><EFBFBD>û<EFBFBD><C3BB>ڲ<EFBFBD><DAB2>Ե<EFBFBD><D4B5><EFBFBD>֤
|
||||
options.AddPolicy("myadmin", policy =>
|
||||
policy.RequireRole("admin"));
|
||||
options.AddPolicy("myadmin", policy =>policy.RequireRole("admin"));
|
||||
|
||||
});
|
||||
|
||||
|
||||
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||||
.AddJwtBearer(options => {
|
||||
.AddJwtBearer(options =>
|
||||
{
|
||||
options.TokenValidationParameters = new TokenValidationParameters
|
||||
{
|
||||
ValidateIssuer = true,//<2F>Ƿ<EFBFBD><C7B7><EFBFBD>֤Issuer
|
||||
ValidateAudience = true,//<2F>Ƿ<EFBFBD><C7B7><EFBFBD>֤Audience
|
||||
ValidateLifetime = true,//<2F>Ƿ<EFBFBD><C7B7><EFBFBD>֤ʧЧʱ<D0A7><CAB1>
|
||||
ClockSkew = TimeSpan.FromSeconds(30),
|
||||
ClockSkew = TimeSpan.FromDays(1),
|
||||
|
||||
|
||||
ValidateIssuerSigningKey = true,//<2F>Ƿ<EFBFBD><C7B7><EFBFBD>֤SecurityKey
|
||||
ValidAudience = JwtConst.Domain,//Audience
|
||||
ValidIssuer = JwtConst.Domain,//Issuer<65><72><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0>ǩ<EFBFBD><C7A9>jwt<77><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
|
||||
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(JwtConst.SecurityKey))//<2F>õ<EFBFBD>SecurityKey
|
||||
};
|
||||
});
|
||||
services.AddControllers();
|
||||
services.AddSwaggerService();
|
||||
services.AddSession();
|
||||
|
||||
//ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD><C4B6><EFBFBD>
|
||||
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
||||
|
||||
//<2F><><EFBFBD>ù<EFBFBD><C3B9><EFBFBD><EFBFBD><EFBFBD>
|
||||
Action<MvcOptions> filters = new Action<MvcOptions>(r => {
|
||||
Action<MvcOptions> filters = new Action<MvcOptions>(r =>
|
||||
{
|
||||
//r.Filters.Add(typeof(DbContextFilter));
|
||||
});
|
||||
services.AddMvc(filters);
|
||||
|
||||
services.AddControllers(filters).AddJsonOptions(options => {
|
||||
|
||||
options.JsonSerializerOptions.Converters.Add(new DatetimeJsonConverter());
|
||||
options.JsonSerializerOptions.Converters.Add(new TimeSpanJsonConverter());
|
||||
|
||||
});
|
||||
services.AddSwaggerService();
|
||||
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD>
|
||||
string connection1 = Configuration["ConnectionStringBySQL"];
|
||||
string connection2 = Configuration["ConnectionStringByMySQL"];
|
||||
string connection3 = Configuration["ConnectionStringBySQLite"];
|
||||
string connection4 = Configuration["ConnectionStringByOracle"];
|
||||
|
||||
//var serverVersion = new MySqlServerVersion(new Version(8, 0, 21));//mysql<71>汾
|
||||
|
||||
services.AddDbContext<DataContext>(options =>
|
||||
{
|
||||
options.UseSqlite(connection3, b => b.MigrationsAssembly("CC.Yi.API"));//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD>
|
||||
//options.UseSqlServer(connection1);//sqlserver<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
//options.UseMySql(connection2, serverVersion);//mysql<71><6C><EFBFBD><EFBFBD>
|
||||
options.UseSqlite(connection3);//sqlite<74><65><EFBFBD><EFBFBD>
|
||||
//options.UseOracle(connection4);//oracle<6C><65><EFBFBD><EFBFBD>
|
||||
});
|
||||
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD>ע<EFBFBD><D7A2>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>Autofac
|
||||
//services.AddScoped(typeof(IBaseDal<>), typeof(BaseDal<>));
|
||||
//services.AddScoped(typeof(IstudentBll), typeof(studentBll));
|
||||
|
||||
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD>Identity<74><79><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֤
|
||||
//services.AddIdentity<result_user, IdentityRole>(options =>
|
||||
// {
|
||||
// options.Password.RequiredLength = 6;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̳<EFBFBD><CCB3><EFBFBD>
|
||||
// options.Password.RequireDigit = false;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
// options.Password.RequireLowercase = false;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Сд<D0A1><D0B4>ĸ
|
||||
// options.Password.RequireNonAlphanumeric = false;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD>
|
||||
// options.Password.RequireUppercase = false;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4>ĸ
|
||||
// //options.User.RequireUniqueEmail = false;//ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>Բ<EFBFBD><D4B2>ظ<EFBFBD>
|
||||
// //options.User.AllowedUserNameCharacters="abcd"//<2F><><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD>
|
||||
//}).AddEntityFrameworkStores<DataContext>().AddDefaultTokenProviders();
|
||||
services.AddCors(options => options.AddPolicy("CorsPolicy",//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
builder =>
|
||||
{
|
||||
@@ -111,10 +118,15 @@ namespace CC.Yi.API
|
||||
//<2F><>ʼ<EFBFBD><CABC>ʹ<EFBFBD>ú<EFBFBD><C3BA><EFBFBD>
|
||||
private void InitData(IServiceProvider serviceProvider)
|
||||
{
|
||||
//var serviceScope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope();
|
||||
|
||||
//var context = serviceScope.ServiceProvider.GetService<DataContext>();
|
||||
//DbContentFactory.Initialize(context);//<2F><><EFBFBD>þ<EFBFBD>̬<EFBFBD><EFBFBD><E0B7BD>ע<EFBFBD><D7A2>
|
||||
using (var serviceScope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope())
|
||||
{
|
||||
var Db = serviceScope.ServiceProvider.GetService<DataContext>();
|
||||
var log = serviceScope.ServiceProvider.GetService<Logger<string>>();
|
||||
if (Init.InitDb.Init(Db))
|
||||
{
|
||||
log.LogInformation("<22><><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD>ɹ<EFBFBD><C9B9><EFBFBD>");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
@@ -123,14 +135,23 @@ namespace CC.Yi.API
|
||||
if (env.IsDevelopment())
|
||||
{
|
||||
app.UseDeveloperExceptionPage();
|
||||
|
||||
//<2F><><EFBFBD>ÿ<EFBFBD><C3BF>ӻ<EFBFBD><D3BB>ӿ<EFBFBD>
|
||||
app.UseSwaggerService();
|
||||
}
|
||||
//<2F><><EFBFBD>þ<EFBFBD>̬<EFBFBD>ļ<EFBFBD>
|
||||
app.UseStaticFiles();
|
||||
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD>쳣<EFBFBD><ECB3A3>
|
||||
app.UseErrorHandling();
|
||||
|
||||
//<2F><><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
app.UseCors("CorsPolicy");
|
||||
app.UseHttpsRedirection();
|
||||
app.UseSession();
|
||||
|
||||
app.UseRouting();
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֤
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
@@ -138,6 +159,8 @@ namespace CC.Yi.API
|
||||
{
|
||||
endpoints.MapControllers();
|
||||
});
|
||||
|
||||
//<2F><>ʼ<EFBFBD><CABC>
|
||||
InitData(app.ApplicationServices);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@ namespace CC.Yi.API
|
||||
builder.RegisterType(typeof(CustomAutofacAop));
|
||||
builder.RegisterGeneric(typeof(BaseDal<>)).As(typeof(IBaseDal<>));
|
||||
builder.RegisterType<studentBll>().As<IstudentBll>().EnableInterfaceInterceptors();//表示注入前后要执行Castle,AOP
|
||||
builder.RegisterType<propBll>().As<IpropBll>().EnableInterfaceInterceptors();//表示注入前后要执行Castle,AOP
|
||||
}
|
||||
}
|
||||
}
|
||||
Binary file not shown.
@@ -7,8 +7,8 @@
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
|
||||
"ConnectionStringBySQL": "server=.;Database=YIDB;UId=sa;PWD=52013142020.",
|
||||
"ConnectionStringByMySQL": "Data Source=.;Database=YIDB;User ID=root;Password=52013142020.;pooling=true;port=3306;sslmode=none;CharSet=utf8;",
|
||||
"ConnectionStringBySQLite": "Filename=YIDB.db"
|
||||
"ConnectionStringBySQL": "server=.;Database=YIDB;UId=sa;PWD=Qz52013142020.",
|
||||
"ConnectionStringByMySQL": "Data Source=49.235.212.122;Database=YIDB;User ID=root;Password=Qz52013142020.;pooling=true;port=3306;sslmode=none;CharSet=utf8;",
|
||||
"ConnectionStringBySQLite": "Filename=YIDB.db",
|
||||
"ConnectionStringByOracle": "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=IP<49><50>ַ)(PORT=<3D>˿ں<CBBF>))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=<3D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)));User Id=<3D><>¼<EFBFBD>˺<EFBFBD>;Password=<3D><><EFBFBD><EFBFBD>;"
|
||||
}
|
||||
|
||||
@@ -14,11 +14,11 @@
|
||||
<targets>
|
||||
<!-- 写入文件配置 -->
|
||||
<!-- write logs to file -->
|
||||
<target xsi:type="File" name="allfile" fileName="Logs\nlog-all-${shortdate}.log"
|
||||
<target xsi:type="File" name="allfile" fileName="Logs/nlog-all-${shortdate}.log"
|
||||
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring} ${newline}" />
|
||||
|
||||
<!-- another file log, only own logs. Uses some ASP.NET core renderers -->
|
||||
<target xsi:type="File" name="ownFile-web" fileName="Logs\nlog-own-${shortdate}.log"
|
||||
<target xsi:type="File" name="ownFile-web" fileName="Logs/nlog-own-${shortdate}.log"
|
||||
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action} ${newline}" />
|
||||
</targets>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user