Buscar contenidos

jueves, 5 de diciembre de 2019

Generación, Ejemplos - ServiceBus & Bearer Tokens OAuth 2.0


Azure Service Bus


Generación Token


            string input_read = "https://Nombre.servicebus.windows.net/NombreTopic/subscriptions/NombreSubscription/messages/head";

            string input_write = "https://Nombre.servicebus.windows.net/NombreTopic/messages";

            string output = GetSasToken(input_read_or_write, "user_reader_or_write", "abc", TimeSpan.FromDays(3650));


  public static string GetSasToken(string resourceUri, string keyName, string key, TimeSpan ttl)
        {
            var expiry = GetExpiry(ttl);
            string stringToSign = HttpUtility.UrlEncode(resourceUri) + "\n" + expiry;
            HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(key));
            var signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));
            var sasToken = String.Format(CultureInfo.InvariantCulture, "SharedAccessSignature sr={0}&sig={1}&se={2}&skn={3}",
            HttpUtility.UrlEncode(resourceUri), HttpUtility.UrlEncode(signature), expiry, keyName);
            return sasToken;
        }

C# RestSharp


Escribir Mensaje


   var client = new RestClient("https://Nombre.servicebus.windows.net/NombreTopic/messages");
            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Action", "NombreDestinoFilter");
            request.AddHeader("Authorization", "SharedAccessSignature ABC123…");
            request.AddParameter("undefined", "{\r\n\"a\": 40,\r\n\"b\": 2190120101\r\n}\r\n", ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);


Lectura Mensaje


var client = new RestClient("https://Nombre.servicebus.windows.net/NombreTopic/subscriptions/NombreSubcription/messages/head");
            var request = new RestRequest(Method.DELETE);
            request.AddHeader("Authorization", "SharedAccessSignatureABC123…");
            IRestResponse response = client.Execute(request);


Bearer Tokens OAuth 2.0


Usando PostMan…

POST

Body > FormData
grant_type
client_credentials
client_secret
ABC123_swagger_client_secret
client_id
ABC123_swagger_client_id
resource
Abc123_azureAD_client_id

Response ej:
{
    "token_type""Bearer",
    "expires_in""3600",
    "ext_expires_in""3600",
    "expires_on""1575565994",
    "not_before""1575562094",
    "resource""b437126d-ABC123…",
    "access_token"" ABC123…"
}

Ejemplo Consumo de un Servicio


POST
Headers
authorization
Bearer ABC123…
Content-Type
application/json-patch+json

Body>Json
{"name":"John", "age":31, "city":"New York"}

No hay comentarios:

Publicar un comentario