LoginController.cs
4.99 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 Infrastructure;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Net.Http.Headers;
using System;
using System.Collections.Generic;
using System.Linq;
using WebApp;
using WebRepository;
namespace WebMvc
{
public class LoginController : Controller
{
private readonly string _appKey = "hhweb";
private IUnitWork _unitWork;
private IAuth _authUtil;
private ICacheContext _cacheContext;
public LoginController(IUnitWork unitWork, IAuth authUtil, ICacheContext cacheContext)
{
_unitWork = unitWork;
_authUtil = authUtil;
_cacheContext = cacheContext;
}
// GET: Login
public ActionResult Index()
{
SysInfo sysInfo = _unitWork.FindSingle<SysInfo>(u => u.AppKey.Equals(_appKey));
ViewBag.CopyRight = sysInfo.Copyright;
ViewBag.SystemNameCn = sysInfo.Title;
ViewBag.SystemNameUs = sysInfo.TitleUs;
ViewBag.LoginLogo = sysInfo.LoginLogo;
return View();
}
[HttpPost]
public string Login(string username, string password)
{
var resp = new Response<List<string>>();
try
{
var result = _authUtil.Login(_appKey, username, password);
if (result.Code == 200)
{
CookieOptions cookieOptions = new CookieOptions();
cookieOptions.Expires = DateTimeOffset.Now.AddDays(10d);
Response.Cookies.Append("Token", result.Token, cookieOptions);
resp.Result = _authUtil.GetUserAccountName(username);
string token = result.Token;
string account = resp.Result[0];
string name = resp.Result[1];
IHeaderDictionary headersDictionary = Request.Headers;
string agent = headersDictionary[HeaderNames.UserAgent].ToString();
string Ip = HttpContext.Connection.RemoteIpAddress.ToString();
if (Ip == "127.0.0.1" || Ip == "::1")
{
Ip = "localhost";
}
DateTime dateTime = DateTime.Now;
SysUserOnline userOnline = _unitWork.FindSingle<SysUserOnline>(u => u.Ipaddr.Equals(Ip));
if (userOnline != null)
{
userOnline.Token = token;
userOnline.LoginTime = dateTime;
userOnline.LastAccessTime = dateTime;
_unitWork.Update(userOnline);
}
else
{
SysUserOnline sysUserOnline = new SysUserOnline
{
Token = token,
Account = account,
Name = name,
Ipaddr = Ip,
Browser = agent,
LoginTime = dateTime,
LastAccessTime = dateTime,
};
_unitWork.Add(sysUserOnline);
}
SysLoginLog sysLoginLog = new SysLoginLog
{
Token = token,
Account = account,
Name = name,
Ipaddr = Ip,
Browser = agent,
LoginTime = dateTime,
};
_unitWork.Add(sysLoginLog);
//登出过期用户
List<SysUserOnline> Table_entitys = _unitWork
.Find<SysUserOnline>(u => DateTime.Compare(DateTime.Now, u.LastAccessTime.Value.AddDays(10)) > 0)
.ToList()//直接获取到数据,避免出现连接重用问题
.MapToList<SysUserOnline>();
foreach (var item in Table_entitys)
{
_unitWork.Delete<SysUserOnline>(u => u.Id == item.Id);
_cacheContext.Remove(item.Token);
}
}
else
{
resp.Code = 500;
resp.Message = result.Message;
}
}
catch (Exception e)
{
resp.Code = 500;
resp.Message = e.Message;
}
return JsonHelper.Instance.Serialize(resp);
}
public ActionResult Logout()
{
var token = Request.Cookies["Token"];
SysUserOnline sysUserOnline = _unitWork.FindSingle<SysUserOnline>(u => u.Token.Equals(token));
if (sysUserOnline != null)
{
_unitWork.Delete(sysUserOnline);
}
_authUtil.Logout();
return RedirectToAction("Index", "Login");
}
}
}