SSOController.cs
2.16 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
// ***********************************************************************
// <summary>
// 基础控制器
// 继承该控制器可以防止未登录查看
// 继承该控制器后,如果想访问控制器中存在,但模块配置里面没有的Action(如:Home/Git),请使用AnonymousAttribute
// </summary>
// ***********************************************************************
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using WebRepository;
namespace WebApp
{
public class SSOController : Controller
{
public const string Token = "Token";
protected LoginInfo _loginInfo;
protected IAuth _authUtil;
public SSOController(IAuth authUtil)
{
_authUtil = authUtil;
AuthStrategyContext authStrategyContext = _authUtil.GetCurrentUser();
if (authStrategyContext != null)
{
_loginInfo = new LoginInfo
{
Id = authStrategyContext.User.Id,
Account = authStrategyContext.User.Account,
Name = authStrategyContext.User.Name,
};
}
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
string userToken = "";
//Token by QueryString
var request = filterContext.HttpContext.Request;
if (request.Cookies[Token] != null) //从Cookie读取Token
{
userToken = request.Cookies[Token];
}
if (string.IsNullOrEmpty(userToken))
{
//直接登录
filterContext.Result = LoginResult("");
return;
}
//验证
if (_authUtil.CheckLogin(userToken) == false)
{
//会话丢失,跳转到登录页面
filterContext.Result = LoginResult("");
return;
}
base.OnActionExecuting(filterContext);
}
public virtual ActionResult LoginResult(string username)
{
return new RedirectResult("/Login/Index");
}
}
}