.Net Core 3.0 Api json web token 中间件签权验证和 CORS 中间件处理跨域请求
2021-01-27 00:15
标签:redirect inject guid sys param username cti password class 第一步:在Nuget上安装"Microsoft.AspNet.WebApi.Cors"包,并对api controller使用[EnableCors]特性以及Microsoft.AspNetCore.Authentication.JwtBearer包 第二步:创建.netcore API项目 /控制器:AuthenticateController 第三步: Helper 第四:JwtSettingsModel 第五:appsettings 第六:Startup 第七:postman请求
.Net Core 3.0 Api json web token 中间件签权验证和 CORS 中间件处理跨域请求 标签:redirect inject guid sys param username cti password class 原文地址:https://www.cnblogs.com/Warmsunshine/p/11957904.htmlusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using static JSON_WEB_Token.Helper;
namespace JSON_WEB_Token.Controllers
{ //跨域标签
[EnableCors("anyPolicy")]
//API接口打上Authorize标签
[Authorize]
[ApiController]
//路由配置
[Route("api/[controller]/[action]")]
public class AuthenticateController : ControllerBase
{
private JwtSettingsModel _jwtSettings;
public AuthenticateController(IOptions
using Microsoft.Extensions.Configuration;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IdentityModel.Tokens.Jwt;
using System.IO;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
namespace JSON_WEB_Token
{
public class Helper
{
public static AccessTokenResponse GetAccessTokenModel(UserData userData, int hours = 24, DateTime? expireTimeSpan = null)
{
JwtSettingsModel _jwtSettings = GetAppsettings
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace JSON_WEB_Token
{
public class JwtSettingsModel
{
//token是谁颁发的
public string Issuer { get; set; }
//token可以给哪些客户端使用
public string Audience { get; set; }
//加密的key 必须是16个字符以上,要大于128个字节
public string SecetKey { get; set; }
}
public class AccessTokenErrModel
{
public string AppId { get; set; }
public string errcode { get; set; }
public string errmsg { get; set; }
public Guid UserGid { get; set; }
}
}
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"JwtSettings": {
"Issuer": "http://localhost:44305",
"Audience": "http://localhost:44305",
"SecetKey": "HelloWorldHelloWorldHelloWorld"
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.IdentityModel.Tokens;
namespace JSON_WEB_Token
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
#region json web token 添加认证服务
//将appsettings.json中的JwtSettings部分文件读取到JwtSettings中
services.Configure
上一篇:WPF-自定义实现步骤条控件
下一篇:C#编程之AES加密(一)
文章标题:.Net Core 3.0 Api json web token 中间件签权验证和 CORS 中间件处理跨域请求
文章链接:http://soscw.com/index.php/essay/47512.html