Esempio n. 1
0
        //Creates new tokens
        public string CreateToken(int userId)
        {
            var payload = new TokenContent
            {
                ExpirationDate = DateTimeOffset.UtcNow.AddHours(336),
                UserId         = userId
            };

            IJwtAlgorithm     algorithm  = new HMACSHA256Algorithm();
            IJsonSerializer   serializer = new JsonNetSerializer();
            IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
            IJwtEncoder       encoder    = new JwtEncoder(algorithm, serializer, urlEncoder);

            var token = encoder.Encode(payload, Secret);

            return(token);
        }
Esempio n. 2
0
        public AuthenticatedDto Authenticate(string inputToken)
        {
            //Verify token
            var          tokenManager = new TokenManager();
            TokenContent tokenContent = tokenManager.VerifyToken(inputToken);

            //Check if verification worked
            if (tokenManager.Errored)
            {
                Errored = true;
                ErrorMessages.Add(tokenManager.ErrorMessage);
                return(null);
            }

            //Refresh token
            string        token = tokenManager.CreateToken(tokenContent.UserId);
            Authorization authorizationData;

            try
            {
                using (mulContext context = new mulContext())
                {
                    Authorizer authorizer = new Authorizer();
                    Users      user       = context.Users.FirstOrDefault(o => o.Id == tokenContent.UserId);
                    authorizationData = authorizer.GetAuthorization(context, user);
                }
            }
            catch (Exception Ex)
            {
                Errored = true;
                ErrorMessages.Add(Ex.Message);
                return(null);
            }

            var output = new AuthenticatedDto
            {
                Token      = token,
                Authorized = authorizationData
            };

            return(output);
        }