示例#1
0
        public static string GetUsername(this HttpContextBase httpContext)
        {
            string username = null;
            JwtUtility <JwtHeader, PayloadIdentity> jwtUtil = new JwtUtility <JwtHeader, PayloadIdentity>(new JwtHeader(), new PayloadIdentity());
            string token = httpContext.GetAuthToken();

            if (token != null)
            {
                PayloadIdentity payloadIdentity = jwtUtil.GetPayload(token);
                username = payloadIdentity.Username;
            }
            return(username);
        }
示例#2
0
        public static void SetAuthorizationCookie(this HttpContextBase httpContext, string username)
        {
            string          key      = Settings.InitSetting.Instance.AuthCookieName;
            DateTime        utcNow   = DateTime.UtcNow;
            PayloadIdentity identity = new PayloadIdentity()
            {
                IssuedAt = DateGenerator.ToUnixTimeStamp(utcNow),
                ExpireAt = DateGenerator.ToUnixTimeStamp(utcNow) + Settings.InitSetting.Instance.AuthorizationTokenLifeSpanInSecond,
                Username = username
            };
            JwtUtility <JwtHeader, PayloadIdentity> jwtUtil = new JwtUtility <JwtHeader, PayloadIdentity>(new JwtHeader(), identity);
            string     token  = jwtUtil.ToString();
            HttpCookie cookie = new HttpCookie(key);

            cookie.HttpOnly = false;
            cookie.Value    = token;
            cookie.Expires  = DateTime.Now.AddSeconds(Settings.InitSetting.Instance.AuthorizationTokenLifeSpanInSecond);
            httpContext.Response.Cookies.Add(cookie);
            AddTokenIssued(username, token, DateGenerator.ZeroUnixTimestamp.AddSeconds(identity.IssuedAt), DateGenerator.ZeroUnixTimestamp.AddSeconds(identity.ExpireAt));
        }
示例#3
0
        public static bool IsAuthorized(string token, bool extendToken)
        {
            JwtUtility <JwtHeader, PayloadIdentity> jwtUtil = new JwtUtility <JwtHeader, PayloadIdentity>(new JwtHeader(), new PayloadIdentity());
            PayloadIdentity payloadIdentity = null;

            try
            {
                if (token != null)
                {
                    payloadIdentity = jwtUtil.GetPayload(token);
                }
            }
            catch (Exception) { }
            bool isAuthorized = (token != null && jwtUtil.IsValid(token) && payloadIdentity != null && !payloadIdentity.isTokenExpired());

            if (isAuthorized && extendToken)
            {
                ExtendTokenIssued(payloadIdentity.Username, token);
            }
            return(isAuthorized);
        }