LoginController.cs
5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using Hh.Mes.Common;
using Hh.Mes.Common.config;
using Hh.Mes.Common.Json;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.Response;
using Hh.Mes.Service;
using Hh.Mes.Service.Logs;
using Hh.Mes.Service.SystemAuth;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Net.Http.Headers;
using System;
using System.Linq;
using System.Reflection;
using System.Text;
namespace WebMvc
{
public class LoginController : Controller
{
#region 属性
private readonly string _appKey = "hhweb";
private readonly IAuth _authUtil;
private readonly FaceLoginService faceLoginService;
private readonly LogService logService;
#endregion
public LoginController(IAuth authUtil, LogService logService, FaceLoginService faceLoginService)
{
_authUtil = authUtil;
this.faceLoginService = faceLoginService;
this.logService = logService;
}
public ActionResult Index()
{
ViewBag.copyright = "Copyright © " + DateTime.Now.ToString("yyyy ") + AppSettings.GetAppSeting("copyright");
return View();
}
/// <summary>
/// 登入
/// </summary>
[HttpPost]
public string Login(string username, string password, string webcam, string idcard, string lineCode)
{
var resp = new Response();
try
{
if (!string.IsNullOrEmpty(idcard))
{
#region 工卡登录
Response faceResult = faceLoginService.IdCardSearchService(idcard);
if (faceResult.Code != 200)
{
resp.Code = 500;
resp.Message = faceResult.Message;
return JsonHelper.Instance.Serialize(resp);
}
username = faceResult.Result.account;
password = Encryption.Decrypt(faceResult.Result.password);
#endregion
}
password = JsEncrypt.DecodeBase64(Encoding.Default, password);
var result = _authUtil.Login(_appKey, username, password);
if (result.Code == 200)
{
resp.Token = result.Token;
resp.Result = result.currentSession;
//写登入日志
var sysLogs = new sys_login_log
{
token = result.Token,
id = result.currentSession.Id,
account = result.currentSession.Account,
name = result.currentSession.Name,
ipaddr = HttpContext.Connection.RemoteIpAddress.ToString(),
browser = Request.Headers[HeaderNames.UserAgent].ToString(),
loginTime = DateTime.Now,
};
logService.loginAfter(sysLogs);
//写cookies
//https://www.cnblogs.com/land/archive/2009/04/10/1433074.html
Response.Cookies.Append(SSOAuthAttribute.token, result.Token);
}
else
{
resp.Code = 500;
resp.Message = result.Message;
}
}
catch (Exception e)
{
resp.Code = 500;
resp.Message = e.Message;
}
return JsonHelper.Instance.Serialize(resp);
}
/// <summary>
/// 第3方登入new
/// 测试地址: https://localhost:5001/Login/OtherLogin?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpbk5hbWUiOiJTeXN0ZW0iLCJJU1NVUkUiOiJkaW1zIiwiZXhwIjoxNjUwNTI3MDQ2LjY2MTY2NjZ9.fJbsXxC0eirEZRW87BxfMULwwMiSMGDtOVZKdeLQHTM
/// </summary>
/// <returns></returns>
public ActionResult OtherLogin(string token)
{
var userInfo = JwtEncryption.Decode(token);
var userInfoJosn = DynamicJson.Parse(userInfo);
Response loginResponseResult = faceLoginService.OtherLoginNew(userInfoJosn.loginName);
if (loginResponseResult.Code != 200)
{
ViewBag.msgInfo = loginResponseResult.Message + "--" + userInfo;
return View();
}
//var herf= sysCompanyService.GetDictionaryDictValue(MethodBase.GetCurrentMethod().Name);
var cookieOptions = new CookieOptions();
Response.Cookies.Append(SSOAuthAttribute.token, loginResponseResult.Token, cookieOptions);
//javascript 输出到页面 写入缓存跳转页面
//ViewBag.js = $@"localStorage.setItem('Account', '{loginResponseResult.Result.Account}');
// localStorage.setItem('Name', '{loginResponseResult.Result.Name}')
// window.location.href = '{herf}'";
return View();
}
/// <summary>
/// 退出
/// </summary>
public ActionResult Logout()
{
var token = Request.Cookies[SSOAuthAttribute.token];
logService.Logout(token);
return RedirectToAction("Index", "Login");
}
}
}