asp.net5.0项目的创建常用的配置
asp.net5.0项目
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | 1.autofac:引用Autofac.Extensions.DependencyInjection program: public static IHostBuilder CreateHostBuilder( string [] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }).UseServiceProviderFactory( new AutofacServiceProviderFactory()); stateup: public void ConfigureContainer(ContainerBuilder builder) { var assemblysServices = Assembly.Load( "Show.Repository" ); var assemblysServices1 = Assembly.Load( "Show.Service" ); builder.RegisterAssemblyTypes(assemblysServices, assemblysServices1).AsImplementedInterfaces().InstancePerLifetimeScope(); } 2.autoMapper:引用AutoMapper.Extensions.Microsoft.DependencyInjection stateup: services.AddAutoMapper(x =>x.CreateMap<Depatment, DepatmentDto>()); services.AddAutoMapper(x =>x.CreateMap<DeptRoleDto, DeptRole>()); repository: mapper.Map<DeptRole>(deptRoleDto); or DB.Depatment.ProjectTo<DepatmentDto>(mapper.ConfigurationProvider).ToList(); 3.自引用循环:引用Microsoft.AspNetCore.Mvc.NewtonsoftJson services.AddControllers().AddNewtonsoftJson(options => { //自引用循环 options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; //json数据的时间格式 options.SerializerSettings.DateFormatString = "yyyy-MM-dd" ; //大小写 options.SerializerSettings.ContractResolver = new DefaultContractResolver(); }); 4.jwt 引用:Microsoft.AspNetCore.Authentication.JwtBearer 应用Jwt var authClaims = new List<Claim> { new Claim(JwtClaimTypes.Id,result.Assess) }; IdentityModelEventSource.ShowPII = true ; //签名秘钥 可以放到json文件中 var authSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration[ "JwtConfig:Key" ])); var token = new JwtSecurityToken( issuer: configuration[ "JwtConfig:Issuer" ], audience: configuration[ "JwtConfig:Audienc" ], expires: DateTime.Now.AddHours(10), claims: authClaims, signingCredentials: new SigningCredentials(authSigningKey, SecurityAlgorithms.HmacSha256)); var handler = new JwtSecurityTokenHandler().WriteToken(token); return new MessageDto() { code = 0, Message = handler }; Api: using Microsoft.AspNetCore.Authorization; [Authorize] SetUp配置: //jwt services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme; }).AddJwtBearer(options => { options.SaveToken = true ; options.RequireHttpsMetadata = false ; options.TokenValidationParameters = new TokenValidationParameters() { ValidateIssuer = true , ValidateAudience = true , ValidateIssuerSigningKey = true , IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration[ "JwtConfig:Key" ])), ValidAudience = Configuration[ "JwtConfig:Audienc" ], ValidIssuer = Configuration[ "JwtConfig:Issuer" ], ValidateLifetime = true , //验证生命周期 RequireExpirationTime = true , //过期时间 ClockSkew = TimeSpan.Zero }; }); 中间件: app.UseAuthentication(); |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术