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();

  

posted @   电器小君子  阅读(75)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示