123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- using Microsoft.AspNetCore.Authorization;
- using System.Text;
- using WMS.BZModels.Dto.HJ.WareHouseDtos;
- using WMS.BZUtil;
- using WMS.BZWeb.Extensions;
- using WMS.Info;
- using WMS.Util;
- namespace WMS.BZWeb.Middleware
- {
- public class AuthorizeMiddleware
- {
- private readonly RequestDelegate _next;
- private readonly IOperator _operator;
- public AuthorizeMiddleware(RequestDelegate next, IOperator ioperator)
- {
- _next = next;
- _operator = ioperator;
- }
- /// <summary>
- /// 执行
- /// </summary>
- /// <param name="httpContext">请求连接</param>
- /// <returns></returns>
- public async Task Invoke(HttpContext httpContext)
- {
- //string url = SetUrl(httpContext);
- var endpoint = httpContext.GetEndpoint();
- if (httpContext.Request.IsAjax() && endpoint != null && endpoint.Metadata.GetMetadata<IAllowAnonymous>() == null)
- {
- // 获取请求值
- if (!httpContext.Request.Headers["token"].IsEmpty())
- {
- string token = httpContext.Request.Headers["token"].ToString();
- var res = _operator.DecodeToken(token);
- if (res == "TokenExpiredException")
- {
- await RespondWithJson(httpContext.Response, new ResInfo { code = EResponseCode.nologin, info = "登录信息过期" });
- return;
- }
- else if (res == "SignatureVerificationException")
- {
- await RespondWithJson(httpContext.Response, new ResInfo { code = EResponseCode.nologin, info = "非法密钥" });
- return;
- }
- else
- {
- var payload = res.ToObject<Payload>();
- WebUtil.SetItem("account", payload.Account);
- WebUtil.SetItem("userId", payload.UserId);
- WebUtil.SetItem("userName", payload.UserName);
- WebUtil.SetItem("WarehouseNo", payload.WarehouseNo);
- WebUtil.SetItem("EncryptTokenNo", payload.EncryptTokenNo);
- }
- }
- else
- {
- await RespondWithJson(httpContext.Response, new ResInfo { code = EResponseCode.nologin, info = "权限验证失败" });
- return;
- }
- }
- else
- {
- if (httpContext.Request.Query.ContainsKey("lrmcode"))
- {
- string mouldeCode = httpContext.Request.Query["lrmcode"];
- WebUtil.SetItem("mouldeCode", mouldeCode);
- }
- if (httpContext.Request.Query.ContainsKey("lraccount"))
- {
- string account = httpContext.Request.Query["lraccount"];
- WebUtil.SetItem("account", account);
- }
- }
- await _next(httpContext);
- return;
- }
- /// <summary>
- /// 设置url地址
- /// </summary>
- /// <param name="httpContext">请求上下文</param>
- /// <returns></returns>
- private string SetUrl(HttpContext httpContext)
- {
- string url = httpContext.Request.Path + httpContext.Request.QueryString.Value;
- WebUtil.SetItem("currentUrl", url);
- return url;
- }
- /// <summary>
- /// 返回请求信息
- /// </summary>
- /// <param name="response">返回头</param>
- /// <param name="data">数据</param>
- /// <returns></returns>
- private async Task RespondWithJson(HttpResponse response, object data)
- {
- response.StatusCode = 200;
- response.ContentType = "application/json;charset=utf-8";
- await response.WriteAsync(data.ToJson(), new UTF8Encoding(false));
- }
- }
- public class MiddlewareEx
- {
- private readonly RequestDelegate _next;
- public MiddlewareEx(RequestDelegate next)
- {
- _next = next;
- }
- /// <summary>
- /// 执行
- /// </summary>
- /// <param name="httpContext">请求连接</param>
- /// <returns></returns>
- public async Task Invoke(HttpContext httpContext)
- {
- await _next(httpContext);
- return;
- }
- }
- }
|