Ejemplo n.º 1
0
        public Task Invoke(HttpContext httpContext)
        {
            //检测是否包含'Authorization'请求头
            if (!httpContext.Request.Headers.ContainsKey("Authorization"))
            {
                return(_next(httpContext));
            }
            var           tokenHeader = httpContext.Request.Headers["Authorization"].ToString();
            TokenModelJwt tm          = JwtHelper.SerializeJWT(tokenHeader);//序列化token,获取授权

            //授权 注意这个可以添加多个角色声明,请注意这是一个 list
            var claimList = new List <Claim>();
            var claim     = new Claim(ClaimTypes.Role, tm.Role);

            claimList.Add(claim);
            var identity  = new ClaimsIdentity(claimList);
            var principal = new ClaimsPrincipal(identity);

            httpContext.User = principal;

            return(_next(httpContext));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Invoke the specified httpContext.
        /// </summary>
        /// <returns>The invoke.</returns>
        /// <param name="httpContext">Http context.</param>
        public Task Invoke(HttpContext httpContext)
        {
            if (!httpContext.Request.Headers.ContainsKey("Authorization"))
            {
                return(next(httpContext));
            }

            var tokenHeader = httpContext.Request.Headers["Authorization"].ToString();
            var tm          = JwtHelper.SerializeJWT(tokenHeader);

            var claimList = new List <Claim>();
            var claim     = new Claim(ClaimTypes.Role, tm.Role);

            claimList.Add(claim);

            var identity  = new ClaimsIdentity(claimList);
            var principal = new ClaimsPrincipal(identity);

            httpContext.User = principal;

            return(next(httpContext));
        }
Ejemplo n.º 3
0
        public Task Invoke(HttpContext httpContext)
        {
            PreProceed(httpContext);

            //检测时否包含"Authorization"请求头
            if (!httpContext.Request.Headers.ContainsKey("Authorization"))
            {
                PostProceed(httpContext);
                return(_next(httpContext));
            }

            //var tokenHeader = httpContext.Request.Headers["Authorization"].ToString();
            var tokenHeader = httpContext.Request.Headers["Authorization"].ToString().Replace("Bearer ", "");

            try
            {
                if (tokenHeader.Length >= 128)
                {
                    //Console.WriteLine($"{DateTimeOffset.Now} token: {tokenHeader}.");
                    TokenModelJwt tm = JwtHelper.SerializeJwt(tokenHeader);

                    //授权
                    var claimList = new List <Claim>();
                    var claim     = new Claim(ClaimTypes.Role, tm.Role);
                    claimList.Add(claim);
                    var identity  = new ClaimsIdentity(claimList);
                    var principal = new ClaimsPrincipal(identity);
                    httpContext.User = principal;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"{DateTimeOffset.Now} middleware wrong: {ex.ToString()}.");
            }

            PostProceed(httpContext);
            return(_next(httpContext));
        }