} // End GetById

        public ActiveToken Create(ActiveToken activeToken, string userName, string currentToken, string SourceIdentifier)
        {
            // Validation
            if (string.IsNullOrWhiteSpace(userName))
            {
                throw new ApplicationException("The username for the current active token can not be null or empty");
            }
            if (string.IsNullOrWhiteSpace(currentToken))
            {
                throw new ApplicationException("The current token  can not be null or empty");
            }

            //if (_context.Users.Any(x => x.Username == user.Username))
            //    throw new AppException("Username \"" + user.Username + "\" is already taken");

            //byte[] passwordHash, passwordSalt;
            //CreatePasswordHash(password, out passwordHash, out passwordSalt);

            activeToken.UserName         = userName;
            activeToken.CurrentToken     = currentToken;
            activeToken.Status           = true;
            activeToken.TokenDate        = DateTime.Now;
            activeToken.SourceIdentifier = SourceIdentifier;
            // Hardcoded by now.... TODO: Fix this hardcoded value
            activeToken.TokenExpireDate = DateTime.Now.AddHours(24);

            _context.ActiveTokens.Add(activeToken);
            _context.SaveChanges();

            return(activeToken);
        } // End Create
Beispiel #2
0
        public void AddToken(User user, string token)
        {
            ActiveToken t = new ActiveToken
            {
                Token  = token,
                UserId = user.Id
            };

            _context.Add(t);
            _context.SaveChanges();
        }
        public static ActiveTokenViewModel ConvertToViewModel(ActiveToken dbModel)
        {
            var viewModel = new ActiveTokenViewModel
            {
                Id               = dbModel.Id,
                UserName         = dbModel.UserName,
                CurrentToken     = dbModel.CurrentToken,
                TokenDate        = dbModel.TokenDate,
                TokenExpireDate  = dbModel.TokenExpireDate,
                SourceIdentifier = dbModel.SourceIdentifier,
                Status           = dbModel.Status
            };

            return(viewModel);
        }
        } // End Create

        public ActiveToken Update(ActiveToken activeTokenParam, string currentToken = null, string SourceIdentifier = null)
        {
            var activeToken = _context.ActiveTokens.Find(activeTokenParam.Id);

            if (activeToken == null)
            {
                throw new ApplicationException("ActiveToken not found");
            }

            if (activeTokenParam.CurrentToken != activeToken.CurrentToken)
            {
                // username has changed so check if the new username is already taken
                if (_context.ActiveTokens.Any(x => x.CurrentToken == activeTokenParam.CurrentToken))
                {
                    throw new ApplicationException("CurrentToken " + activeTokenParam.CurrentToken + " is already taken");
                }
            }


            // Update user properties

            activeToken.CurrentToken = activeTokenParam.CurrentToken;
            activeToken.UserName     = activeTokenParam.UserName;
            activeToken.TokenDate    = new DateTime();
            // Hardcoded by now.... TODO: Fix this hardcoded value
            activeToken.TokenExpireDate  = DateTime.Now.AddHours(24);
            activeToken.SourceIdentifier = activeTokenParam.SourceIdentifier;

            // Update password if it was entered

            //if(!string.IsNullOrWhiteSpace(password))
            //{
            //    byte[] passwordHash, passwordSalt;
            //    CreatePasswordHash(password, out passwordHash, out passwordSalt);

            //    user.PasswordHash = passwordHash;
            //    user.PasswordSalt = passwordSalt;

            //}

            _context.ActiveTokens.Update(activeToken);
            _context.SaveChanges();
            return(activeToken);
        } // End Update
Beispiel #5
0
        public async Task <ActionResult <TokenResponse> > PostActiveToken(ActiveToken activeToken)
        {
            ////ActiveToken activeToken = new ActiveToken
            ////{
            ////    Otp = content
            ////};
            _context.ActiveToken.Add(activeToken);
            await _context.SaveChangesAsync();

            //Check Otp and return token

            TokenResponse tokenresponse = new TokenResponse();

            tokenresponse.TokenValue   = "memel";
            tokenresponse.ResponseCode = "00";
            tokenresponse.ResponseMsg  = "OK";

            return(tokenresponse);
            //return CreatedAtAction("GetActiveToken", new { id = activeToken.Id }, activeToken);
        }
Beispiel #6
0
        public User GetUserFromToken(string token)
        {
            var t = new JwtSecurityTokenHandler().ReadJwtToken(token);

            if (t.ValidTo > DateTime.Now)
            {
                ActiveToken dbToken = _context.ActiveTokens.Where(i => i.Token == token).FirstOrDefault();
                if (dbToken == null)
                {
                    return(null);
                }
                else
                {
                    return(_context.Users.Where(i => i.Id == dbToken.UserId).FirstOrDefault());
                }
            }
            else
            {
                return(null);
            }
        }
        public async Task CreateToken(ActiveToken token)
        {
            await _context.ActiveToken.AddAsync(token);

            await _context.SaveChangesAsync();
        }
Beispiel #8
0
 public static System.Net.Http.HttpClient AddActiveToken(this System.Net.Http.HttpClient client)
 {
     client.DefaultRequestHeaders.Add("AuthorizeToken", ActiveToken.ToString());
     return(client);
 }
Beispiel #9
0
 public void MoveActiveToken()
 {
     ActiveToken.MoveSpaces(dice.Result);
 }
Beispiel #10
0
 public bool IsAuthenticated()
 {
     return(ActiveToken.IsActive());
 }