protected override Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { HttpStatusCode statusCode; string token; // determine whether a jwt exists or not if (!TryRetrieveToken(request, out token)) { statusCode = HttpStatusCode.Unauthorized; return(base.SendAsync(request, cancellationToken)); } try { var secretKey = Utilerias.JWT_Secret_Key(); var audienceToken = Utilerias.JWT_Audience_Token(); var issuerToken = Utilerias.JWT_Issuer_Token(); var securityKey = new SymmetricSecurityKey(Encoding.Default.GetBytes(secretKey)); SecurityToken securityToken; var tokenHandler = new System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler(); TokenValidationParameters validationParameters = new TokenValidationParameters() { ValidAudience = audienceToken, ValidIssuer = issuerToken, ValidateLifetime = true, ValidateIssuerSigningKey = true, LifetimeValidator = LifetimeValidator, IssuerSigningKey = securityKey }; // Extract and assign Current Principal and user Thread.CurrentPrincipal = tokenHandler.ValidateToken(token, validationParameters, out securityToken); HttpContext.Current.User = tokenHandler.ValidateToken(token, validationParameters, out securityToken); return(base.SendAsync(request, cancellationToken)); } catch (SecurityTokenValidationException) { statusCode = HttpStatusCode.Unauthorized; } catch (Exception) { statusCode = HttpStatusCode.InternalServerError; } return(Task <HttpResponseMessage> .Factory.StartNew(() => new HttpResponseMessage(statusCode) { })); }
// Mostrar lista de blogs en Base de Datos. public static List <Blog> GetBlogs() { lock (syncRoot) { var _oSb = new StringBuilder(); var _oLst = new List <Blog>(); _oSb.Clear().AppendFormat("SELECT t1.idBlog, t1.comentarioBlog, t1.tipoComentario, t1.nombreProducto, "); _oSb.AppendFormat("t1.autorComentario, t1.fechaAlta, t1.fechaActualizacion, t1.flgPrioritario, t1.flgAlta, "); _oSb.AppendFormat("t1.flgEdicion, t1.flgEliminado FROM [Blogs_Angular]..[mtBlogs] t1;"); using (var oDb = new DBManager(Utilerias.ConnectionString())) { _oLst = oDb.GetDataToMapping <Blog>(_oSb.ToString(), System.Data.CommandType.Text, null); } // Fin de la conexión a Base de Datos. return(_oLst); } }
// Mostrar una cuenta de usuario existente. public static User GetUser(string userName) { lock (syncRoot) { var _oSb = new StringBuilder(); var _oLst = new User(); var _oParam = new List <System.Data.IDbDataParameter>(); _oSb.Clear().AppendFormat("SELECT t1.idUser, t1.userName, t1.fechaAlta, t1.fechaActualizacion, t1.flgIsLogged, t1.flgEliminado FROM [Blogs_Angular]..[mtUsuarios] t1 WHERE (t1.userName = @UserName);"); using (var oDb = new DBManager(Utilerias.ConnectionString())) { _oParam.Add(oDb.CreateParameter("@UserName", userName, System.Data.DbType.String)); _oLst = oDb.GetDataToMapping <User>(_oSb.ToString(), CommandType.Text, _oParam.ToArray()).Single(); } // Fin de la conexión a Base de Datos. return(_oLst); } }
// Mostrar un blog. public static Blog GetBlog(int id) { lock (syncRoot) { var _oSb = new StringBuilder(); var _oLst = new Blog(); var _oParam = new List <System.Data.IDbDataParameter>(); _oSb.Clear().AppendFormat("SELECT t1.idBlog, t1.comentarioBlog, t1.tipoComentario, t1.nombreProducto, "); _oSb.AppendFormat("t1.autorComentario, t1.fechaAlta, t1.fechaActualizacion, t1.flgPrioritario, t1.flgAlta, "); _oSb.AppendFormat("t1.flgEdicion, t1.flgEliminado FROM [Blogs_Angular]..[mtBlogs] t1 where (t1.idBlog = @IdBlog);"); using (var oDb = new DBManager(Utilerias.ConnectionString())) { _oParam.Add(oDb.CreateParameter("@IdBlog", id, System.Data.DbType.Int64)); _oLst = oDb.GetDataToMapping <Blog>(_oSb.ToString(), System.Data.CommandType.Text, _oParam.ToArray()).Single(); } // Fin de la conexión a Base de Datos. return(_oLst); } }