zoukankan      html  css  js  c++  java
  • netcore 用户阻止重复登录 互踢

     public class RestrictUserMiddleware
        {
            public readonly RequestDelegate _next;
            private ILogger _logger;
            public ICacheService cacheService;
    
            public RestrictUserMiddleware(RequestDelegate next, ILogger<GlobalExceptionCatchMiddleware> logger, IServiceProvider service)
            {
                _next = next;
                _logger = logger;
                cacheService = (MemoryCacheService)service.GetService(typeof(MemoryCacheService));
            }
    
            public async Task Invoke(HttpContext context)
            {
                var user = context.User.Claims.Where(i => i.Type == ConfigHelper.Claim_UserName).FirstOrDefault();
                var path = context.Request.Path.Value;
                //呼叫端用户互踢处理
                if (user != null && path.Contains("xxxxService/CallingClient"))
                {
                    var token = context.Request.Headers["Authorization"].ToString();
                    var username = user.Value;
                    if (cacheService.Exists(username))
                    {
                        var c_token = cacheService.GetValue(username);
                        var exists = cacheService.Exists(token);
                        if (exists)
                        {
                            context.Response.Clear();
                            context.Response.StatusCode = StatusCodes.Status200OK;
                            var responseResult = ResponseResult<object>.Expire("Expire");
                            var responseStr = JsonConvert.SerializeObject(responseResult, Formatting.None, new JsonSerializerSettings { ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver() });
                            context.Response.ContentType = "application/json;charset=utf-8";
                            await context.Response.WriteAsync(responseStr);
                        }
                        else if (token != c_token)
                        {
                            cacheService.Add(c_token, 1);
                            cacheService.Add(username, token);
                        }
                    }
                    else
                    {
                        cacheService.Add(username, token);
                    }
                }
                await _next(context);
            }
        }
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {//使用Token验证
                app.UseAuthentication();
                app.UseAuthorization();
                app.UseRestrictUserMiddleware();
    }

    cache记录token,旧token,则阻止防护正确结果

  • 相关阅读:
    BZOJ2962: 序列操作
    BZOJ2037: [Sdoi2008]Sue的小球
    LOJ#2537. 「PKUWC2018」Minimax
    LOJ#2538. 「PKUWC2018」Slay the Spire
    BZOJ4756 [USACO17JAN]Promotion Counting晋升者计数
    BZOJ2212——线段树合并
    atcoder.keyence2019.contest E-Connecting Cities
    [转载]笛卡尔树
    大数模板
    点分治
  • 原文地址:https://www.cnblogs.com/huanyun/p/15271517.html
Copyright © 2011-2022 走看看