分类 Net core 下的文章

如错误:
System.Web.Http.WebHost, Version=5.2.9.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”的程序集“System.Web.Http.WebHost”所使用的“System.Web.Http, Version=5.2.9.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”版本高于所引用的标识为“System.Web.Http, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”的程序集“System.Web.Http”

NUGET 执行:
Update-Package Microsoft.AspNet.WebApi -reinstall
Update-Package Microsoft.AspNet.Mvc -reinstall

如果安装了太高级别的版本,想要降版本,在nuget管理器中一般很难操作成功,在命令行中操作即可,直接安装指定版本即可
Install-Package Microsoft.AspNet.WebApi -Version 5.2.9

删除包指令:
Uninstall-Package Microsoft.Net.Http
Uninstall-Package Microsoft.Bcl

我们的项目从.net framework 转到 net core webapi,不想改太多代码,特别是身份认证,之前是直接在 QueryString 中传输 token 来认证,各个 action 上有个自定义的 CheckLogin 标签,要改成的 JWT 认证体系,工作量巨大,于是想到利用过滤器来实现自定义的身份认证过程。

先建一个空的 Attribute,不用任何功能,有这个 Attribute 的 Action 表示都需要认证

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class LoginCheckAttribute : Attribute
{
}

然后建一个 IActionFilter

public class AuthFilter : IActionFilter
{
    private const string UnauthorizedMessage = "授权失效或过期,请重新登录。";

    public void OnActionExecuting(ActionExecutingContext context)
    {
        var controllerInfo = context.ActionDescriptor as ControllerActionDescriptor;
        var needcheck = controllerInfo?.EndpointMetadata.Any(_ => _.GetType() == typeof(LoginCheckAttribute)) ?? false;
        if (needcheck)
        {
            var token = context.HttpContext.Request.Query["token"].ToString();

            ApiResult<object>? result = null;
            if (string.IsNullOrEmpty(token))
            {
                result = new() { Code = 888, Message = UnauthorizedMessage };
            }
            else
            {
                var uinfo = Factory.SessionAccess.GetUserByToken(token);//根据token得到用户信息
                if (uinfo == null)
                {
                    result = new() { Code = 888, Message = UnauthorizedMessage };
                }
                else
                {
                    if (uinfo.State != (int)AdminState.enable)
                    {
                        result = new() { Code = 888, Message = "账号无法使用" };
                    }
                    else
                        context.HttpContext.Items["user"] = uinfo;//用户信息存在会话中,供业务逻辑中调用
                }
            }

            if(result != null)
            {
                context.Result = new UnauthorizedObjectResult(result);
            }
        }
    }

    public void OnActionExecuted(ActionExecutedContext context)
    {
    }
}

然后在 program.cs 中加入过滤器:

builder.Services.AddControllers().AddMvcOptions(options => options.Filters.Insert(0, new AuthFilter()));

业务逻辑中需要用到用户信息的地方这样获取:

    public UserDetails? UserInfo
    {
        get
        {
            if (MyHttpContext.Current == null) return null;
            if(MyHttpContext.Current.Items.TryGetValue("user", out var value))
            {
                return value as UserDetails;
            }
            return null;
        }
    }

直接用 扩展

    public static string GetRemoteIPAddress(this HttpContext context, bool allowForwarded = true)
    {
        if (allowForwarded)
        {
            string? header = context.Request.Headers["CF-Connecting-IP"].FirstOrDefault() ?? context.Request.Headers["X-Forwarded-For"].FirstOrDefault();
            if (!string.IsNullOrEmpty(header) && IPAddress.TryParse(header, out var ip))
            {
                return ip.ToString();
            }
        }
        var _ip = context.Connection.RemoteIpAddress;

        string res = "";
        if (_ip != null)
        {
            if (_ip.IsIPv4MappedToIPv6)
                res = _ip.MapToIPv4().ToString();
            else
                res = _ip.ToString();
        }
        return res;
    }

使用:

string ip = HttpContext.GetRemoteIPAddress();

开发技术上,微软走下坡路的一大原因,除了开源生态不够,还有一个很重要的原因,很多常用的功能,不能简单的代码实现,这是很多微软系程序猿头疼一件事。

比如,要想做个简单的 http server,需要很多代码,最后,性能跟IIS还相差十万八千里
比如,要搭个FTP,非要搞得很复杂,跟系统用户挂钩,不能简单的用户名、密码、目录
比如,要做个最简单的md5加密,还得好几行代码,还不是常见的写法,每次得去搜索
比如,得到一个时间戳,也是一样得好几行代码,敲很多文字
比如,读取数据库到对象,还要自己去实现 DataTableToEntityList
…………

太多了,最近在使用 net core 开发 控制台应用程序时,为了读取一个 json 配置文件,被折磨得不行了。。。。
网上的资源都是比较老的,读取时,无法解决错误:

IConfiguration builder = new ConfigurationBuilder()

这个简单的语句,怎么都是提示
错误 CS0246 未能找到类型或命名空间名“ConfigurationBuilder”(是否缺少 using 指令或程序集引用?)

我加了这些 Nuget 引用:

Microsoft.Extensions.Configuration
Microsoft.Extensions.Configuration.Json
Microsoft.Extensions.Configuration.EnvironmentVariables
Microsoft.Extensions.Configuration.FileExtensions

仍然没用,还是找不到!
真无语,这种常用的功能,居然要用微软自己的东西来实现这么麻烦,于是手动写了一个,而且增加了监控功能,当文件有修改时,自动读取新的配置。

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace CommProcess
{
    /// <summary>
    /// Json 配置读取类
    /// </summary>
    public class JsonConfig
    {
        private JObject JsonObj;
        private readonly string JsonFilePath;
        private readonly FileSystemWatcher JsonWatcher;

        /// <summary>
        /// Json 配置读取类
        /// </summary>
        /// <param name="JsonFilePath">Json 文件路径,绝对或相对路径</param>
        /// <param name="MonitorChange">是否监视配置文件改动</param>
        public JsonConfig(string JsonFilePath, bool MonitorChange = true)
        {
            this.JsonFilePath = JsonFilePath;
            ReadFile();

            if (MonitorChange)
            {
                if (JsonFilePath.IndexOf(':') != 1)
                {
                    JsonFilePath = Path.Combine(Environment.CurrentDirectory, JsonFilePath);
                }
                JsonWatcher = new FileSystemWatcher();
                JsonWatcher.Path = Path.GetDirectoryName(JsonFilePath);
                JsonWatcher.Filter = Path.GetFileName(JsonFilePath);
                JsonWatcher.EnableRaisingEvents = true;
                JsonWatcher.Changed += JsonWatcher_Changed;
            }
        }

        private void JsonWatcher_Changed(object sender, FileSystemEventArgs e)
        {
            Task.Run(() =>
            {
                Task.Delay(1000);
                ReadFile();
            });
        }

        private void ReadFile()
        {
            using var stream = File.Open(JsonFilePath, FileMode.Open, FileAccess.Read);
            byte[] b = new byte[stream.Length];
            stream.Read(b, 0, b.Length);
            string json = Encoding.UTF8.GetString(b);
            JsonObj = JObject.Parse(json);
        }

        /// <summary>
        /// 获取配置值
        /// </summary>
        /// <typeparam name="T">数据类型</typeparam>
        /// <param name="node">路径,比如:config.server</param>
        /// <returns></returns>
        public T? Get<T>(string node)
        {
            var jnode = JsonObj.SelectToken(node);
            if (jnode == null) return default;
            return jnode.Value<T>();
        }
    }
}

使用很简单,

var jconfig = new JsonConfig("jsconfig.json");
var name = jconfig.Get<string>("info.name");

Session 是比较老的服务端单会话缓存实现,在新的 net core 中如果需要使用 session,需要增加服务注入,其实直接使用内置的 TempData 替代使用更方便。

TempData 是在会话有效的过程中长期有效的,只是在正常获取一次值 TempData[key] 后,就会被删除。但可以简单的通过:

value = TempData.Peek(key)

的方式来获取值,就不会被删除,等同于 Session 的效果了。