using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using CNet.Main.BLL;
using CNet.Common;
namespace CNet.Web.Api.Controllers
{
///
/// 认证
///
[Route("api/[controller]")]
//jwt1 身份认证
public class AuthroizeController:Controller
{
private readonly JwtSeetings _jwtSeetings;
public AuthroizeController(IOptions jwtSeetingsOptions)
{
_jwtSeetings = jwtSeetingsOptions.Value;
}
///
/// 登录获取token
///
/// 登录实体信息
///
[HttpPost,AllowAnonymous]
public ActionResult Post([FromBody]LoginViewModel loginViewModel)
{
//if (!ModelState.IsValid)
//{
// return BadRequest();
//}
var users = new Pub_UserBLL().GetList($"StopFlag=0 AND UserName='{loginViewModel.Name}' AND UserPwd='{loginViewModel.Password}'", limits: 1);
if (users.Count>0)
{
var user = users.First();
//var userFunctions = new Pub_UserfunctionBLL().GetList($"UserCode='{user.UserCode}'").Select(p=>p.FunctionCode);
//var roleFunctions = new Pub_RolefunctionBLL().GetList($" RoleCode IN(SELECT pur.RoleCode FROM Pub_Userrole AS pur WHERE pur.UserCode='{user.UserCode}' )").Select(p=>p.FunctionCode);
//var functions = userFunctions.Concat(roleFunctions).Distinct();
//var functionsStr = string.Join(',', functions);
var claims = new Claim[]
{
new Claim(ClaimTypes.Name,user.UserName),
new Claim("Id",user.Id.ToString()),
new Claim("UserCode",user.UserCode),
new Claim("Tel",user.Tel??""),
new Claim("DeptCode",user.DeptCode??"")
};
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtSeetings.SecretKey));
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
var expires = DateTime.Now.AddMinutes(30);
var token = new JwtSecurityToken(
_jwtSeetings.Issuer,
_jwtSeetings.Audience,
claims,
DateTime.Now,
expires,
creds
);
return Ok(new ResponseObj()
{
Code = 1,
Message = "认证成功",
Data = new { Token = new JwtSecurityTokenHandler().WriteToken(token),
Expires = TypeUtil.ConvertDateTimeInt(expires) }
});
}
return Ok(new ResponseObj()
{
Code = 0,
Message = "用户名密码错误!"
});
//return BadRequest();
}
}
}