Blame view

Hh.Mes.Api/Controllers/BaseController.cs 3.65 KB
1
2
using Hh.Mes.Common;
using Hh.Mes.Common.Json;
赖素文 authored
3
4
5
using Hh.Mes.Pojo.System;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
6
using Microsoft.AspNetCore.Mvc.Filters;
7
8
using System;
using System.Collections.Generic;
唐召明 authored
9
using System.Text.Json;
赖素文 authored
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

namespace Hh.Mes.Api.Controllers
{
    public class BaseController : ControllerBase
    {
        /// <summary>
        /// 上下文
        /// </summary>
        public HttpContext context;

        /// <summary>
        /// 获取用户 
        /// 注意配置文件(appsettings)设置【RedisDb】要和中控系统【RedisDb】保持一致,否则取不到用户信息
        /// </summary>
        public UserAuthSession GetUser(HttpContext httpContext)
        {
26
            try
唐召明 authored
27
            {
28
29
30
31
32
33
                var token = GetToken(httpContext);
                if (string.IsNullOrWhiteSpace(token))
                {
                    return null;
                }
                var json = JwtEncryption.Decode(token);
唐召明 authored
34
                return JsonSerializer.Deserialize<UserAuthSession>(json);
35
36
            }
            catch (Exception)
唐召明 authored
37
            {
38
                return null;
唐召明 authored
39
            }
赖素文 authored
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
        }

        /// <summary>
        /// 获取 tokens
        /// </summary>
        public string GetToken(HttpContext httpContext)
        {
            var token = httpContext.Request.Query[Program.tokens];
            if (string.IsNullOrEmpty(token)) token = httpContext.Request.Cookies[Program.tokens];
            if (string.IsNullOrEmpty(token)) token = httpContext.Request.Headers[Program.tokens];
            return token;
        }

        public string Serialize(object result)
        {
            return JsonHelper.Instance.Serialize(result);
        }

        /// <summary> 
        /// 获取客户端ip 注意先依赖注入context
        /// </summary>
        /// <returns></returns>
        public string GetIp()
        {
            string ip = context.Connection.RemoteIpAddress.ToString();
            return ip;
        }
67
68
69
70
71
72
73
74
75
    }

    /// <summary>
    /// 用户验证 ,在全局忽略下,做二次保险验证
    /// </summary>
    public class ValidateUserAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext context)
        {
76
            try
唐召明 authored
77
            {
78
79
80
81
82
83
84
85
86
                string token = context.HttpContext.Request.Query[Program.tokens];
                if (string.IsNullOrEmpty(token)) token = context.HttpContext.Request.Cookies[Program.tokens];
                if (string.IsNullOrEmpty(token)) token = context.HttpContext.Request.Headers[Program.tokens];
                if (string.IsNullOrWhiteSpace(token))
                {
                    ContextResponse(context);
                    return;
                }
                var json = JwtEncryption.Decode(token);
唐召明 authored
87
                var user = JsonSerializer.Deserialize<UserAuthSession>(json);
88
89
                // 可以将用户信息存储到 HttpContext.Items 供后续使用
                context.HttpContext.Items["Account"] = user.Account;
90
            }
91
            catch (Exception)
唐召明 authored
92
            {
93
94
95
96
97
                ContextResponse(context);
                return;
            }
            base.OnActionExecuting(context);
        }
赖素文 authored
98
99
100
101
102
103
104
105
106
107
108
        private void ContextResponse(ActionExecutingContext context)
        {
            context.HttpContext.Response.ContentType = "application/json";
            context.Result = new ContentResult
            {
                Content = "{\"code\":401,\"status\":false,\"message\":\"Tokens失效 请重新登入\",\"result\":null}",
                StatusCode = StatusCodes.Status200OK,
                ContentType = "application/json"
            };
        }
赖素文 authored
109
110
    }
}