Exemplo n.º 1
0
        public JWTAuthenticationToken GetNewAccessTokenFromRefreshToken(string refreshToken, ApplicationDbContext ctx)
        {
            JwtSecurityToken token = ValidateToken(refreshToken);

            if (token == null)
            {
                throw new BlogException("tokenInvalid", new string[] { token.ToString() });
            }
            string hashedUserId = token.Claims.First((c) => c.Type == ClaimTypes.NameIdentifier).Value;
            string UserId       = Base64UrlEncoder.Decode(hashedUserId);

            var query = (from a in ctx.User where a.UserId == UserId select a);

            if (query.Count() == 0)
            {
                throw new BlogException("userNotFound", new string[] { UserId });
            }
            else
            {
                User   u               = query.First();
                string newToken        = CreateAccessToken(u.UserName, UserId, u.UserRole);
                string newRefreshToken = CreateRefreshToken(UserId);
                JWTAuthenticationToken newAuthToken = new JWTAuthenticationToken();
                newAuthToken.Token        = newToken;
                newAuthToken.RefreshToken = newRefreshToken;
                newAuthToken.User         = u;
                return(newAuthToken);
            }
        }
        public BaseRestApiInterface validateLogin([FromBody] JWTAuthenticationToken oldTokens)
        {
            BaseRestApiResult result = new BaseRestApiResult();

            result.process((ctx) => {
                var token = jwtAuth.ValidateToken(oldTokens.Token);
                if (token == null)
                {
                    return(jwtAuth.GetNewAccessTokenFromRefreshToken(oldTokens.RefreshToken, ctx));
                }
                else
                {
                    oldTokens.User = jwtAuth.GetUserFromAccessToken(token, true);
                }
                return(oldTokens);
            });
            return(result);
        }
        public BaseRestApiInterface doLogin([FromBody] User oldUser)
        {
            BaseRestApiResult result = new BaseRestApiResult();

            result.process((ctx) => {
                var query = (from u in ctx.User where u.UserId.ToLower() == oldUser.UserId.ToLower() && u.Password == oldUser.Password select u).ToList();
                if (query.Count == 0)
                {
                    throw new BlogException("userNotFound", new string[] { oldUser.UserId });
                }
                else
                {
                    User u                        = query.First();
                    string token                  = jwtAuth.CreateAccessToken(u.UserName, u.UserId, u.UserRole);
                    string refresh_token          = jwtAuth.CreateRefreshToken(u.UserId);
                    JWTAuthenticationToken tokens = new JWTAuthenticationToken();
                    tokens.Token                  = token;
                    tokens.RefreshToken           = refresh_token;
                    tokens.User                   = u;
                    return(tokens);
                }
            });
            return(result);
        }