QQ交流群:110826636

.NET Core 3.1+Vue的前后端分离部署方案

使用nginx进行部署,下载地址:https://nginx.org/en/download.html

.NET Core 3.1,下载地址:https://dotnet.microsoft.com/download/dotnet-core

部署环境安排,可以是linux中的centos或者Ubuntu,我准备了win2012 r2(建议用win2016,2012需要打补丁支持.NET Core),方便不懂运维的同事可以操作(哈哈)

.NET Core 案例项目如下:本地运行后访问http://localhost:4000/api/user/auth/login 测试用get,发布走post

 

 

 

    [Area("user")]
    [Route("api/[area]/[controller]")]
    [ApiController]
    public class AuthController : ControllerBase
    {
        private IUserService _userService;

        public AuthController(IUserService userService)
        {
            _userService = userService;
        }

        [HttpPost("login")]
        //[HttpGet("login")]
        public IActionResult Login(string username, string password)
        {
            var tokenHandler = new JwtSecurityTokenHandler();
            var key = Encoding.ASCII.GetBytes("aaaaaabbbbbbcccc");
            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new[] { new Claim("id", "1") }),
                Expires = DateTime.UtcNow.AddDays(7),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
            };
            var token = tokenHandler.CreateToken(tokenDescriptor);
            string tokenStr = tokenHandler.WriteToken(token);
            return Ok(new  {
                access_token= tokenStr,
                token_type ="bearer",
                expires_in=7*60
            });
            //return Ok(token);
        }
    }

Vue案例项目:Vuetify做UI组件,其他是全家桶

 

 

 

 使用nginx进行部署:内部对/api下的请求做代理,让他去访问WebApi,80给Vue部署前台,这是最简单配置,如果你对头信息有要求可以在把头信息加上

 

 两个都跑起来,这边WebApi自宿主,就不在安装IIS了,前台的请求到了内部就转发了,

 

 

 

 

 

posted @ 2020-10-30 14:29  FourOne  阅读(236)  评论(0编辑  收藏