当前位置:网站首页>ASP. NET CORE Study11
ASP. NET CORE Study11
2022-06-28 12:35:00 【When the human stars shine】
Simple personal blog project construction notes
1. summary
Project outline
By making a simple , Easy to understand personal blog project , It is very simple to add, delete, modify and check , To learn better asp.net core, This project uses asp.net core webapi+elementui To do it .
2. Database design
Article table
ID
Article title
Article content
Creation time
Type of article ID
Browse volume
Like
author ID
Article type table
ID
Type name
Author's list
ID
full name
account number
password MD5
3. Architecture design
Storage layer
Service layer
MD5 encryption
public static string MD5Encrypt32(string password)
{
string pwd = "";
MD5 md5 = MD5.Create(); // Instantiate a md5 Antithetic image
byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(password));
for (int i = 0; i < s.Length; i++)
{
pwd = pwd + s[i].ToString("X");
}
return pwd;
}
JWT Use
JWT to grant authorization
1. Add one webapi project
2. install Nuget Package System.IdentityModel.Tokens.Jwt
var claims = new Claim[]
{
new Claim(ClaimTypes.Name, " Zhang San ")
};
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("SDMC-CJAS1-SAD-DFSFA-SADHJVF-VF"));
//issuer On behalf of Token Of Web Applications ,audience yes Token Accepted by
var token = new JwtSecurityToken(
issuer: "http://localhost:6060",
audience: "http://localhost:5000",
claims: claims,
notBefore: DateTime.Now,
expires: DateTime.Now.AddHours(1),
signingCredentials: new SigningCredentials(key, SecurityAlgorithms.HmacSha256)
);
var jwtToken = new JwtSecurityTokenHandler().WriteToken(token);
return jwtToken;
JWT authentication
install Microsoft.AspNetCore.Authentication.JwtBearer
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("SDMC-CJAS1-SAD-DFSFA-SADHJVF-VF")),
ValidateIssuer = true,
ValidIssuer = "http://localhost:6060",
ValidateAudience = true,
ValidAudience = "http://localhost:5000",
ValidateLifetime = true,
ClockSkew = TimeSpan.FromMinutes(60)
};
});
JWT Authorized authentication use
Swagger If you want to use authentication, you need to register the service, add the following code
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
In=ParameterLocation.Header,
Type=SecuritySchemeType.ApiKey,
Description= " Type... Directly in the box below Bearer {token}( Notice that there is a space between the two )",
Name="Authorization",
BearerFormat="JWT",
Scheme="Bearer"
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference=new OpenApiReference
{
Type=ReferenceType.SecurityScheme,
Id="Bearer"
}
},
new string[] {
}
}
});
AutoMapper
install Nuget AutoMapper.Extensions.Microsoft.DependencyInjection
Define a class , Inherit Profile
public class CustomAutoMapperProfile:Profile
{
public CustomAutoMapperProfile()
{
base.CreateMap<StudentEntity, StudentDto>();
}
}
Register in the service
services.AddAutoMapper(typeof(CustomAutoMapperProfile));
Constructor injection
private readonly IMapper _mapper;
public StudentsController(IMapper mapper)
{
this._mapper = mapper;
}
Complex mapping
base.CreateMap<Admin, AdminDto>()
.ForMember(dest => dest.RoleMsg, sourse => sourse.MapFrom(src => src.RoleInfo.RoleMsg));
User:
UserPwd -> Cannot return to the front end
UserName -> Back to front end
UserDTO:
UserName
边栏推荐
- UGUI使用小技巧(五) Scroll Rect组件的使用
- JS duration and asynchronous function promise
- .NET混合开发解决方案24 WebView2对比CefSharp的超强优势
- group_ Concat learning and configuration
- UGUI强制刷新Layout(布局)组件
- 【Unity编辑器扩展实践】、通过代码查找所有预制
- Unity webgl mobile end removal warning
- 【Unity编辑器扩展基础】、EditorGUILayout(二)
- 【编解码】从零开始写H264解码器(1) 总纲
- Unity Editor Extension Foundation, guilayout
猜你喜欢
Deep learning has a new pit! The University of Sydney proposed a new cross modal task, using text to guide image matting
ASP.NET CORE Study03
ASP.NET CORE Study02
[C language] use of nested secondary pointer of structure
Mathematical principle derivation of structured light phase shift method + multifrequency heterodyne
In less than an hour, apple destroyed 15 startups
[C language] about scanf() and scanf_ Some problems of s()
unity发布 webgl在手机端 inputfield唤醒键盘输入
企业源代码保密方案分享
【Unity编辑器扩展实践】、通过代码查找所有预制
随机推荐
Usage and principle of precomputedtextcompat
KDD 2022 | 图“预训练、提示、微调”范式下的图神经网络泛化框架
.NET混合开发解决方案24 WebView2对比CefSharp的超强优势
Jerry's wif interferes with Bluetooth [chapter]
NFT数字藏品系统开发(3D建模经济模型开发案例)
SHA256加密工具类
华泰证券开户安全吗? 开户有风险吗
【Unity编辑器扩展基础】、GUI
[C language] use of file read / write function
深度学习又有新坑了!悉尼大学提出全新跨模态任务,用文本指导图像进行抠图...
Unity Editor Extension Foundation, editorguilayout (II)
企业源代码保密方案分享
我的NVIDIA开发者之旅-Jetson Nano 2gb教你怎么训练模型(完整的模型训练套路)
双缓冲绘图
Truly understand triode beginner level chapter (Classic) "suggestions collection"
Unity导入资源后还手动修改资源的属性?这段代码可以给你节约很多时间:AssetPostprocessor
设置Canvas的 overrideSorting不生效
ASP.NET CORE Study06
group_ Concat learning and configuration
【Unity编辑器扩展实践】、通过代码查找所有预制