Example #1
0
        public async Task <IActionResult> CustomLogin([FromBody] UserModel userModel)
        {
            var user = userDbContext.Users.FirstOrDefault(u => u.Username == userModel.Username);

            if (user != null)
            {
                if (user.Password == userModel.Password.Sha256())
                {
                    var token = new Entities.Token
                    {
                        Id         = Guid.NewGuid().ToString(),
                        Expiration = DateTime.Now + TimeSpan.FromMinutes(30),
                        Owner      = userModel.Username,
                        Role       = user.Role
                    };
                    tokenDbContext.Add(token);
                    tokenDbContext.SaveChanges();
                    return(Ok(token.Id));
                }
                else
                {
                    return(StatusCode(500, "Wrong password"));
                }
            }
            return(StatusCode(500, "User not found"));
        }
Example #2
0
        public ServerResult <User> StartToken(string tokenString)
        {
            try
            {
                Entities.Token token = tokenRepository.FindByToken(tokenString);

                if (token.endTime < DateTime.Now)
                {
                    return(new ServerResult <User>
                    {
                        Success = false,
                        Message = "Token has reached end time ",
                        Data = null,
                    });
                }

                return(new ServerResult <User>
                {
                    Success = true,
                    Data = new User
                    {
                        Token = tokenString,
                        EndTime = token.endTime
                    },
                });
            }
            catch (Exception e)
            {
                return(new ServerResult <User>
                {
                    Success = false,
                    Message = e.Message,
                });
            }
        }
Example #3
0
 public Token(Entities.Token token)
 {
     this.Email          = token.Email;
     this.TokenData      = token.TokenData;
     this.ExpirationDate = token.ExpirationDate;
     this.CreationDate   = token.CreationDate;
     this.DeviceName     = token.DeviceName;
     this.Blacklisted    = token.Blacklisted;
 }
Example #4
0
        public void SaveToken(Item item, Entities.Token token)
        {
            item.Editing.BeginEdit();

            item[Templates.Account.AccessToken]    = token.AccessToken;
            item[Templates.Account.TokenType]      = token.TokenType;
            item[Templates.Account.ExpiresIn]      = token.ExpiresIn.ToString(CultureInfo.InvariantCulture);
            item[Templates.Account.ExpirationDate] = DateUtil.ToIsoDate(DateTime.UtcNow.AddSeconds(token.ExpiresIn), false, true);

            item.Editing.EndEdit();
        }
Example #5
0
 public Entities.Token ToEntity()
 {
     Entities.Token entity = new Entities.Token
     {
         Email          = Email,
         TokenData      = TokenData,
         ExpirationDate = ExpirationDate,
         CreationDate   = CreationDate,
         DeviceName     = DeviceName,
         Blacklisted    = Blacklisted
     };
     return(entity);
 }
        private void SaveTokenToDB(string _email, JwtSecurityToken _token, DateTime _iat, DateTime _exp, string _deviceName)
        {
            Entities.Token t = new Entities.Token
            {
                TokenData      = new JwtSecurityTokenHandler().WriteToken(_token),
                Email          = _email,
                CreationDate   = _iat,
                ExpirationDate = _exp,
                DeviceName     = _deviceName
            };

            _dalProvider.SaveToken(t);
            //var tt1 = dalProvider.GetToken("*****@*****.**"); //get token
        }
        public override async Task StoreAsync(string key, AuthorizationCode code)
        {
            var efCode = new Entities.Token
            {
                Key       = key,
                SubjectId = code.SubjectId,
                ClientId  = code.ClientId,
                JsonCode  = ConvertToJson(code),
                Expiry    = DateTime.UtcNow.AddSeconds(code.Client.AuthorizationCodeLifetime),
                TokenType = _tokenType
            };

            _context.Tokens.Add(efCode);
            await _context.SaveChangesAsync();
        }
        public override async Task StoreAsync(string key, Token value)
        {
            var efToken = new Entities.Token
            {
                Key       = key,
                SubjectId = value.SubjectId,
                ClientId  = value.ClientId,
                JsonCode  = ConvertToJson(value),
                Expiry    = DateTime.UtcNow.AddSeconds(value.Lifetime),
                TokenType = this._tokenType
            };

            _context.Tokens.Add(efToken);
            await _context.SaveChangesAsync();
        }
        public async Task StoreReferenceTokenAsync(string handle, Token token)
        {
            var storeToken = new Entities.Token
            {
                Key       = handle,
                SubjectId = token.SubjectId,
                ClientId  = token.ClientId,
                JsonCode  = JsonConvert.SerializeObject(token, GetJsonSerializerSettings()),
                Expiry    = DateTime.UtcNow.AddSeconds(token.Lifetime),
                TokenType = Entities.TokenType.TokenHandle
            };

            _context.Tokens.Add(storeToken);
            await((DbContext)_context).SaveChangesAsync();
        }
        public async Task StoreAuthorizationCodeAsync(string handle, AuthorizationCode code)
        {
            var token = new Entities.Token
            {
                Key       = handle,
                SubjectId = code.Subject.Claims.FirstOrDefault(c => c.Type == "sub")?.Value,
                ClientId  = code.ClientId,
                JsonCode  = JsonConvert.SerializeObject(code, GetJsonSerializerSettings()),
                Expiry    = DateTime.UtcNow.AddSeconds(code.Lifetime),
                TokenType = Entities.TokenType.AuthorizationCode,
            };

            _context.Tokens.Add(token);
            await((DbContext)_context).SaveChangesAsync();
        }
Example #11
0
        public static Guid CreateToken(int userId, int validForMinutes = 3)
        {
            DeleteExpiredTokens();
            using (var context = GetMMBContext())
            {
                var newToken = new Entities.Token
                {
                    TokenGuid = Guid.NewGuid(),
                    UserId    = userId,
                    ValidTill = CommonFunctions.GetCurrentDateTime().AddMinutes(validForMinutes)
                };

                context.Token.Add(newToken);
                context.SaveChanges();
                return(newToken.TokenGuid);
            }
        }
        public override async Task StoreAsync(string key, RefreshToken value)
        {
            var token = await _context.Tokens
                        .Where(x => x.Key == key && x.TokenType == _tokenType)
                        .FirstOrDefaultAsync();

            if (token == null)
            {
                token = new Entities.Token
                {
                    Key       = key,
                    SubjectId = value.SubjectId,
                    ClientId  = value.ClientId,
                    JsonCode  = ConvertToJson(value),
                    TokenType = _tokenType
                };
                _context.Tokens.Add(token);
            }

            token.Expiry = DateTime.UtcNow.AddSeconds(value.LifeTime);
            await _context.SaveChangesAsync();
        }
        public async Task StoreRefreshTokenAsync(string handle, RefreshToken refreshToken)
        {
            var token = await _context.Tokens
                        .Where(x => x.Key == handle && x.TokenType == Entities.TokenType.RefreshToken)
                        .FirstOrDefaultAsync();

            if (token == null)
            {
                token = new Entities.Token
                {
                    Key       = handle,
                    SubjectId = refreshToken.SubjectId,
                    ClientId  = refreshToken.ClientId,
                    JsonCode  = JsonConvert.SerializeObject(refreshToken, GetJsonSerializerSettings()),
                    TokenType = Entities.TokenType.RefreshToken,
                };
                _context.Tokens.Add(token);
            }

            token.Expiry = DateTime.UtcNow.AddSeconds(refreshToken.Lifetime);
            await((DbContext)_context).SaveChangesAsync();
        }