一、后端接口:WebApi
1.appsettings.json文件
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"Authentication": {
"SecretKey": "JianKeEducationCRMAPI",
"Issuer": "******",
"Audience": "******"
},
"AllowedHosts": "*"
}
2.Startup.cs
需要的命名空间
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
ConfigureServices需要修改
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
var secretByte = Encoding.UTF8.GetBytes(Configuration["Authentication:SecretKey"]);
options.TokenValidationParameters = new TokenValidationParameters()
{
ValidateIssuer = true,
ValidIssuer = Configuration["Authentication:Issuer"],
ValidateAudience = true,
ValidAudience = Configuration["Authentication:Audience"],
ValidateLifetime = true,
IssuerSigningKey = new SymmetricSecurityKey(secretByte)
};
});
Configure里必须有
app.UseAuthorization();
3.登录方法
[ApiController]
[Route("api/[controller]/[action]")]
public class UserController : ControllerBase
{
private readonly IConfiguration _configuration;
public UserController(IConfiguration configuration)
{
_configuration = configuration;
}
[HttpPost]
public JsonResult Login([FromBody] UserModel userModel)
//public string Login(string userName, string password)
{
if (userModel.userName=="1" && userModel.password=="1