Skip to content

Instantly share code, notes, and snippets.

View waldyrfelix's full-sized avatar

Waldyr Felix waldyrfelix

View GitHub Profile
using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using WebApiContrib.Internal;
namespace WebApiContrib.MessageHandlers
{
@waldyrfelix
waldyrfelix / regex-domain-to-www-domain.cs
Last active December 15, 2015 14:59
redirects domain.com to www.domain.com
if (Regex.IsMatch(Request.Url.AbsoluteUri, @"^((http|https)://[^www]).*$"))
{
string newDomain = String.Format("{0}://www.{1}{2}",
Request.Url.Scheme,
Request.Url.Authority,
Request.Url.AbsolutePath);
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", newDomain);
Response.End();
@waldyrfelix
waldyrfelix / gist:3993683
Created November 1, 2012 13:39
Implementado BasicAuthenticationAttribute
public class AppBasicAuthenticationAttribute : BasicAuthenticationAttribute
{
public override string Realm { get { return "DOMINIO"; }}
public override bool Authorize(string username, string password)
{
var membershipService = IoC.Current.Resolve<IMembershipService>();
return membershipService.ValidateUser(username, password);
}
}
@waldyrfelix
waldyrfelix / gist:3983411
Created October 30, 2012 22:10
Requisição HTTP Basic Authorization com JS
$.ajax({
url: "http://localhost:36210/api/values",
type: "GET",
dataType: "json",
beforeSend: function(xhr){
xhr.setRequestHeader('Authorization', 'Basic d2FsZHlyOjEyMw==');
},
success: function(data) {
$(data).each(function(index, value){
console.log(index + ') '+ value);
@waldyrfelix
waldyrfelix / gist:3983405
Created October 30, 2012 22:09
Requisição HTTP Basic Authorization com C#
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", "Basic d2FsZHlyOjEyMw==");
var strings = await client.GetAsync("/api/values");
var strResult = await strings.Content.ReadAsStringAsync();
var serializer = new JavaScriptSerializer();
var result = serializer.Deserialize<string[]>(strResult);
foreach (var s in result)
@waldyrfelix
waldyrfelix / gist:3908170
Created October 17, 2012 21:03
Criptografia no AES no Windows 8
private static readonly byte[] iv = // array of 16 bytes
public static string Encrypt(string message, string password)
{
var algorithm = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7);
var key = algorithm.CreateSymmetricKey(Encoding.UTF8.GetBytes(password).AsBuffer());
var encrypted = CryptographicEngine.Encrypt(key, Encoding.UTF8.GetBytes(message).AsBuffer(), iv.AsBuffer());
return CryptographicBuffer.EncodeToBase64String(encrypted);
}
@waldyrfelix
waldyrfelix / gist:3908162
Created October 17, 2012 21:02
AES Mode CBC with PKCS7 Padding
private static readonly byte[] iv = // array de 16 bytes
private static string decrypt(string message, string key)
{
var encryptedBytes = Convert.FromBase64String(message);
var aesAlg = makeAesCryptoServiceProvider(key);
var decryptor = aesAlg.CreateDecryptor();
using (var memory = new MemoryStream(encryptedBytes))
@waldyrfelix
waldyrfelix / gist:3709328
Created September 12, 2012 19:34
Para liberar arquivos presos no TFS Preview
tf undo $/Root/Folder/file.txt /workspace:"Maquina";[email protected] /s:https://SERVER.tfspreview.com/DefaultCollection
@waldyrfelix
waldyrfelix / gist:3093920
Created July 11, 2012 21:59
Mock para verificar sequencia de chamadas (iniciar transação, inserir um pacote de desconto, finalizar transação)
[TestMethod]
public void CadastrarPacoteDeDesconto_deve_chamar_o_repositorio_inserindo_o_pacotes_de_descontos_numa_transação()
{
// arrange
var pacote = new PacoteDesconto();
var mockUnitOfWork = TestHelper.MockUnitOfWork();
using (Sequence.Create())
{
@waldyrfelix
waldyrfelix / gist:3093834
Created July 11, 2012 21:48
Mock de um UnitOfWork retornada a partir de um ServiceLocator
public static Mock<IUnitOfWork> MockUnitOfWork()
{
var mockUnitOfWork = new Mock<IUnitOfWork>();
var mockUnitOfWorkFactory = new Mock<IUnitOfWorkFactory>();
mockUnitOfWorkFactory.Setup(_ => _.Criar())
.Returns(mockUnitOfWork.Object);
var mockServiceLocator = new Mock<IServiceLocator>();
mockServiceLocator.Setup(_ => _.GetInstance<IUnitOfWorkFactory>())