Ejemplo n.º 1
0
        // GET /autenticacao/
        public Autenticado Get(string token, int colecao = 0, int campo = 0, int orderBy = 0, int pageSize = 0, int pageNumber = 0)
        {
            // Abre nova conexão
            using (painel_taxservices_dbContext _db = new painel_taxservices_dbContext())
            {
                try
                {
                    if (!Permissoes.Autenticado(token, _db))
                    {
                        throw new HttpResponseException(HttpStatusCode.Unauthorized);
                    }

                    //_db.Configuration.ProxyCreationEnabled = false;

                    var verify = (from v in _db.LoginAutenticacaos
                                  where v.token.Equals(token)
                                  select v
                                  ).Single();

                    if (verify == null)
                    {
                        throw new HttpResponseException(HttpStatusCode.InternalServerError);
                    }

                    #region Log de Acesso ao Sistema
                    api.Models.Object.Log log = new api.Models.Object.Log();
                    log.IdUser       = verify.idUsers;
                    log.IdController = 45;
                    log.IdMethod     = 51;
                    log.DtAcesso     = DateTime.Now;

                    LogAcesso.New(log);
                    #endregion

                    return(AcessoUsuarioLogado(token, verify.idUsers, _db));
                }
                catch (Exception e)
                {
                    if (e.Message.Equals("401"))
                    {
                        throw new HttpResponseException(HttpStatusCode.Unauthorized);
                    }
                    else
                    {
                        throw new HttpResponseException(HttpStatusCode.InternalServerError);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        // POST /autenticacao
        public Autenticado Post(Models.Object.Login data)
        {
            try
            {
                if (ModelState.IsValid && WebSecurity.Login(data.usuario, data.senha, persistCookie: false))
                {
                    // Abre nova conexão
                    using (painel_taxservices_dbContext _db = new painel_taxservices_dbContext())
                    {
                        int userId = WebSecurity.GetUserId(data.usuario);

                        try
                        {
                            //_db.Configuration.ProxyCreationEnabled = false;


                            #region Log de Acesso ao Sistema
                            api.Models.Object.Log log = new api.Models.Object.Log();
                            log.IdUser       = userId;
                            log.IdController = 45;
                            log.IdMethod     = 50;
                            log.DtAcesso     = DateTime.Now;

                            LogAcesso.New(log);
                            #endregion

                            string token = "";



                            var verify = (from v in _db.LoginAutenticacaos
                                          where v.idUsers.Equals(userId)
                                          orderby v.idUsers
                                          select v
                                          ).FirstOrDefault();

                            if (verify == null)
                            {
                                token = Token.GetUniqueKey(data.usuario);
                                LoginAutenticacao la = new LoginAutenticacao();
                                la.idUsers    = userId;
                                la.token      = token;
                                la.dtValidade = DateTime.Now;
                                _db.LoginAutenticacaos.Add(la);
                                _db.SaveChanges();
                            }
                            else
                            {
                                token = verify.token;
                            }


                            return(AcessoUsuarioLogado(token, userId, _db));
                        }
                        catch (Exception e)
                        {
                            throw new HttpResponseException(HttpStatusCode.InternalServerError);
                        }
                    }
                }
                else
                {
                    throw new Exception("Usuário e/ou senha inválidos!" + (ModelState.IsValid ? "" : " (invalid model)"));
                }
            }
            catch (Exception e)
            {
                if (e.Message.Equals("401"))
                {
                    throw new HttpResponseException(HttpStatusCode.Unauthorized);
                }
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.InternalServerError, e.InnerException == null ? e.Message : e.InnerException.InnerException == null ? e.InnerException.Message : e.InnerException.InnerException.Message));
                //return Request.CreateResponse(HttpStatusCode.InternalServerError, e.Message);
            }
        }