Beispiel #1
0
        public HttpResponseMessage Authenticate(string username, string password)
        {
            webinarModel webinar = new webinarModel();
            var          user    = webinar.Users.First(x => x.Username == username && x.Password == password);

            if (user != null)
            {
                return(GetAuthToken(user.UserId));
            }
            return(Request.CreateResponse(HttpStatusCode.BadRequest, "Not authorized"));
        }
        public bool ValidateToken(string tokenId)
        {
            webinarModel webinarModel = new webinarModel();
            var          token        = webinarModel.Tokens.FirstOrDefault(x => x.AuthToken == tokenId && x.ExpiresOn > DateTime.Now);

            if (token != null && !(DateTime.Now > token.ExpiresOn))
            {
                token.ExpiresOn = DateTime.Now.AddDays(5);
                webinarModel.Entry(token).State = EntityState.Modified;
                webinarModel.SaveChanges();
                return(true);
            }
            return(false);
        }
        public bool DeleteByUserId(int userId)
        {
            webinarModel webinarModel = new webinarModel();
            var          tokens       = webinarModel.Tokens.Where(x => x.UserId == userId);

            foreach (Tokens token in tokens)
            {
                webinarModel.Entry(token).State = EntityState.Deleted;
                webinarModel.SaveChanges();
            }
            if (webinarModel.Tokens.Where(x => x.UserId == userId).ToList().Count == 0)
            {
                return(true);
            }
            return(false);
        }
        public bool Kill(string tokenId)
        {
            webinarModel webinarModel = new webinarModel();
            var          tokens       = webinarModel.Tokens.Where(x => x.AuthToken == tokenId);

            foreach (Tokens token in tokens)
            {
                webinarModel.Entry(token).State = EntityState.Deleted;
                webinarModel.SaveChanges();
            }
            if (webinarModel.Tokens.Where(x => x.AuthToken == tokenId).ToList().Count == 0)
            {
                return(true);
            }
            return(false);
        }
        public Tokens GenerateToken(int userId)
        {
            webinarModel webinarModel = new webinarModel();
            string       token        = Guid.NewGuid().ToString();
            DateTime     issuedOn     = DateTime.Now;
            DateTime     expiredOn    = DateTime.Now.AddDays(5);
            var          tokenDomain  = new Tokens
            {
                UserId    = userId,
                AuthToken = token,
                IssuedOn  = issuedOn,
                ExpiresOn = expiredOn
            };

            webinarModel.Tokens.Add(tokenDomain);
            webinarModel.SaveChanges();
            return(tokenDomain);
        }