Пример #1
0
        /// <summary>
        /// Parses the Authorization header and creates user credentials
        /// </summary>
        /// <param name="actionContext"></param>
        protected virtual AuthenticationIdentity AuthHeader(HttpActionContext actionContext)
        {
            string authHeader = null;
            var    auth       = actionContext.Request.Headers.Authorization;

            if (auth != null && auth.Scheme == "Bearer")
            {
                authHeader = auth.Parameter;
            }
            if (string.IsNullOrEmpty(authHeader))
            {
                return(null);
            }

            var jwtKey = System.Configuration.ConfigurationManager.AppSettings["jwtKey"];

            SymmetricAlgorithm     sa   = new SymmetricAlgorithm(SymmetricAlgorithm.Tipo.TripleDES);
            AuthenticationIdentity user = null;

            try
            {
                authHeader = JWT.JsonWebToken.Decode(authHeader, sa.Decrypt(jwtKey));
                user       = JsonConvert.DeserializeObject <AuthenticationIdentity>(authHeader);
            }
            catch
            {
            }

            return(user);
        }
Пример #2
0
        protected virtual void LoadUser(AuthenticationIdentity user, HttpActionContext actionContext)
        {
            if (HttpContext.Current != null)
            {
                BaseApiController baseApiController = actionContext.ControllerContext.Controller as BaseApiController;

                if (baseApiController != null)
                {
                    // Chave padrão do cache - nome do método + parâmetros.
                    string chave = RetornaChaveCache_LoadUser(user);
                    object cache = HttpContext.Current.Cache[chave];

                    if (cache == null)
                    {
                        #region Load user values

                        UsuarioWEB userLogged = new UsuarioWEB();

                        // Carrega usuário na session através do ticket de authenticação
                        userLogged.Usuario = new SYS_Usuario
                        {
                            ent_id = user.Entity
                            ,
                            usu_login = user.Login
                        };
                        SYS_UsuarioBO.GetSelectBy_ent_id_usu_login(userLogged.Usuario);

                        userLogged.Grupo = SYS_GrupoBO.GetEntity(new SYS_Grupo {
                            gru_id = user.Group
                        });

                        baseApiController.__userLogged = userLogged;

                        #endregion

                        HttpContext.Current.Cache.Insert(chave, userLogged, null, DateTime.Now.AddMinutes(GestaoEscolarUtilBO.MinutosCacheMedio)
                                                         , System.Web.Caching.Cache.NoSlidingExpiration);
                    }
                    else
                    {
                        baseApiController.__userLogged = cache as UsuarioWEB;
                    }
                }
            }
        }
Пример #3
0
 private string RetornaChaveCache_LoadUser(AuthenticationIdentity entity)
 {
     return(string.Format("LoadUserJWK_{0}_{1}_{2}", entity.Entity, entity.Login, entity.Group));
 }