예제 #1
0
        protected GuerillaBlockClassBase(string className)
        {
            TokenDictionary = new TokenDictionary();
            TokenDictionary.Add(className);
            TargetUnit = new CodeCompileUnit();
            var tagsCodeNamespace = new CodeNamespace("Moonfish.Guerilla.Tags");

            tagsCodeNamespace.Imports.Add(new CodeNamespaceImport("Moonfish.Tags"));
            tagsCodeNamespace.Imports.Add(new CodeNamespaceImport("Moonfish.Model"));
            tagsCodeNamespace.Imports.Add(new CodeNamespaceImport("System.IO"));
            tagsCodeNamespace.Imports.Add(new CodeNamespaceImport("System.Collections.Generic"));
            tagsCodeNamespace.Imports.Add(new CodeNamespaceImport("System.Linq"));
            TargetClass = new CodeTypeDeclaration(className)
            {
                TypeAttributes = TypeAttributes.Public
            };
            tagsCodeNamespace.Types.Add(TargetClass);
            TargetUnit.Namespaces.Add(tagsCodeNamespace);
        }
예제 #2
0
        private TokenDescription GenerateToken(User user)
        {
            var tokenHandle = new JwtSecurityTokenHandler();
            var date        = DateTime.UtcNow;
            var expired     = date.AddMinutes(20);

            var modules = new List <Claim>();

            modules.Add(new Claim("id", user.Id.ToString()));
            modules.Add(new Claim("username", user.Username));
            foreach (var module in user.UsersModules)
            {
                modules.Add(new Claim("module", module.Module.Module));
            }
            try
            {
                var tokenDescriptor = new SecurityTokenDescriptor
                {
                    Subject  = new ClaimsIdentity(modules),
                    Expires  = expired,
                    Audience = _appSettings.Audience,
                    Issuer   = _appSettings.Issuer,
                    EncryptingCredentials = new X509EncryptingCredentials(new X509Certificate2(_appSettings.PublicKey)),
                };
                var token = new TokenDescription
                {
                    Value   = tokenHandle.CreateEncodedJwt(tokenDescriptor),
                    Now     = date,
                    Expired = expired
                };
                TokenDictionary.Add(user.Id, token);
                return(token);
            }
            catch (System.Exception)
            {
                return(null);
            }
        }