Beispiel #1
0
        public async Task <string> HelloUser(TokenJwt token, string tenant)
        {
            //TAS on-behalf API -> get saml key from auth token
            var attrs = await tasClient.GetWellKnownSamlAttributes(tenant, token.Sub.SamlKey);

            return($"Hello, {attrs.TasPersonalGivenName}");
        }
Beispiel #2
0
        public JsonResult GetJwtStr(long id = 1, string role = "")
        {
            //这里就是用户登陆以后,通过数据库去调取数据,分配权限的操作
            var tokenModel = new TokenJwt
            {
                Uid  = id,
                Role = role
            };

            string jwtStr = JwtHelper.IssueJwt(tokenModel);

            return(Json(jwtStr));
        }
Beispiel #3
0
        /// <summary>
        /// 解析
        /// </summary>
        public static TokenJwt SerializeJwt(string jwtStr)
        {
            var jwtHandler            = new JwtSecurityTokenHandler();
            JwtSecurityToken jwtToken = jwtHandler.ReadJwtToken(jwtStr);
            object           role;

            try
            {
                jwtToken.Payload.TryGetValue("Role", out role);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
            var tm = new TokenJwt
            {
                Uid  = Convert.ToInt32(jwtToken.Id),
                Role = role != null?role.ToString() : "",
            };

            return(tm);
        }
Beispiel #4
0
        public Task Invoke(HttpContext httpContext)
        {
            //检测是否包含'Authorization'请求头
            if (!httpContext.Request.Headers.ContainsKey("Authorization"))
            {
                return(_next(httpContext));
            }
            string tokenHeader = httpContext.Request.Headers["Authorization"].ToString();
            //序列化token,获取授权
            TokenJwt tokenJwt = JwtHelper.SerializeJwt(tokenHeader);

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

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

            httpContext.User = principal;

            return(_next(httpContext));
        }
Beispiel #5
0
        /// <summary>
        /// 颁发JWT字符串
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public static string IssueJwt(TokenJwt token)
        {
            var dateTime = DateTime.UtcNow;
            var claims   = new []
            {
                new Claim(JwtRegisteredClaimNames.Jti, token.Uid.ToString()), //Id
                new Claim("Role", token.Role),                                //角色
                new Claim(JwtRegisteredClaimNames.Iat, dateTime.ToString(CultureInfo.InvariantCulture), ClaimValueTypes.Integer64)
            };
            //秘钥
            var key   = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(JwtHelper.SecretKey));
            var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

            var jwt = new JwtSecurityToken(
                issuer: "Blog.Core",
                claims: claims, //声明集合
                expires: dateTime.AddHours(2),
                signingCredentials: creds);

            var    jwtHandler = new JwtSecurityTokenHandler();
            string encodedJwt = jwtHandler.WriteToken(jwt);

            return(encodedJwt);
        }
Beispiel #6
0
 public string HelloNonSot(TokenJwt token)
 {
     //Ca = Consuming app
     return($"Hello, {token.Cons.Ca}");
 }