Skip to content

Instantly share code, notes, and snippets.

View waldyrfelix's full-sized avatar

Waldyr Felix waldyrfelix

View GitHub Profile
@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: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: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: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 / 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();
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
{