ASP.NET Core 项目实战(Login登录接口)

  1. 创建登录控制器接口


    image.png
using Interface;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model.Dto.User;
using WebApi.Config;
using WebModel.Other;

namespace WebApi.Controllers
{
    /// 
    /// 登录控制器
    /// 
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class LoginController : ControllerBase
    {
        private IUserService _userService; // 用户信息
        private ICustomJWTService _jwtService;// token
        /// 
        /// 构造类
        /// 
        /// 
        /// 
        public LoginController(IUserService userService, ICustomJWTService jwtService)
        {
            _userService = userService;
            _jwtService = jwtService;
        }

        /// 
        /// 登录用户
        /// 
        /// 用户名
        /// 密码
        /// 
        [HttpGet]
        public async Task GetToken(string name, string password)
        {

            var res = Task.Run(() =>
            {
                if(string.IsNullOrEmpty(name) || string.IsNullOrEmpty(password))
                {
                    return ResultHelper.Error("参数不能为空");
                }
                UserRes user = _userService.GetUser(name, password);
                if (string.IsNullOrEmpty(user.Name))
                {
                    return ResultHelper.Error("账号不存在,用户名或密码错误!");
                }
                return ResultHelper.Success(_jwtService.GerToken(user));
            });
            return await res;

        }


    }
}

  1. 运行项目 得到 token
image.png
  1. 好了 简单的项目搭建就到这里了

你可能感兴趣的:(ASP.NET Core 项目实战(Login登录接口))