![Page 1: Trilha .NET - REST na plataforma Microsoft com ASP.NET Web API](https://reader033.vdocument.in/reader033/viewer/2022061114/545c9c17af7959b90e8b4835/html5/thumbnails/1.jpg)
Globalcode – Open4education
Trilha - .NETREST na plataforma Microsoft com ASP.NET Web API
Waldyr Felix
MVP, MCPD, MCP e Arquiteto de Software
http://waldyrfelix.net
@WaldyrFelix
![Page 2: Trilha .NET - REST na plataforma Microsoft com ASP.NET Web API](https://reader033.vdocument.in/reader033/viewer/2022061114/545c9c17af7959b90e8b4835/html5/thumbnails/2.jpg)
Globalcode – Open4education
Uso de REST no mundo
![Page 3: Trilha .NET - REST na plataforma Microsoft com ASP.NET Web API](https://reader033.vdocument.in/reader033/viewer/2022061114/545c9c17af7959b90e8b4835/html5/thumbnails/3.jpg)
Globalcode – Open4education
Entrega de conteúdo
em vários dispositivos,
na prática basta um
dispositivo ter acesso
a internet para poder
consumir um serviço REST.
![Page 4: Trilha .NET - REST na plataforma Microsoft com ASP.NET Web API](https://reader033.vdocument.in/reader033/viewer/2022061114/545c9c17af7959b90e8b4835/html5/thumbnails/4.jpg)
Globalcode – Open4education
REST
Recurso a ser acessado
Formato 1Formato
2Formato
3Formato
4
REST Web Service
URI
Métodos
GET POST PUT DELETE
![Page 5: Trilha .NET - REST na plataforma Microsoft com ASP.NET Web API](https://reader033.vdocument.in/reader033/viewer/2022061114/545c9c17af7959b90e8b4835/html5/thumbnails/5.jpg)
Globalcode – Open4education
Porque ASP.NET Web API
Precisamos entregar conteúdo para múltiplos dispositivos
A web está cada vez mais baseada no browser e menos no server
É mais fácil escalar usando REST
Abraça o HTTP, não precisamos de novos protocolos
Mesmo modelo de programação do ASP.NET MVC
Compatível com os frameworks de Single Page Application
![Page 6: Trilha .NET - REST na plataforma Microsoft com ASP.NET Web API](https://reader033.vdocument.in/reader033/viewer/2022061114/545c9c17af7959b90e8b4835/html5/thumbnails/6.jpg)
Globalcode – Open4education
Um framework atual
• Compatibilidade com qualquer dispositivo móvel• Pode ser facilmente consumido por qualquer linguagem
de programação• Use JSON preferencialmente• Suporta vários tipos de autenticação
![Page 7: Trilha .NET - REST na plataforma Microsoft com ASP.NET Web API](https://reader033.vdocument.in/reader033/viewer/2022061114/545c9c17af7959b90e8b4835/html5/thumbnails/7.jpg)
Globalcode – Open4education
ASP.NET Web API & OData
OData é um padrão de consulta de dados na web
Dá maior flexibilidade na busca dos dados
É nativo no ASP.NET WebAPI
+
![Page 8: Trilha .NET - REST na plataforma Microsoft com ASP.NET Web API](https://reader033.vdocument.in/reader033/viewer/2022061114/545c9c17af7959b90e8b4835/html5/thumbnails/8.jpg)
Globalcode – Open4education
Demo, demo, demo, demo…
![Page 9: Trilha .NET - REST na plataforma Microsoft com ASP.NET Web API](https://reader033.vdocument.in/reader033/viewer/2022061114/545c9c17af7959b90e8b4835/html5/thumbnails/9.jpg)
Globalcode – Open4education
Exemplo em C#
using (var client = new HttpClient()){ 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) { Console.WriteLine(s); }}
![Page 10: Trilha .NET - REST na plataforma Microsoft com ASP.NET Web API](https://reader033.vdocument.in/reader033/viewer/2022061114/545c9c17af7959b90e8b4835/html5/thumbnails/10.jpg)
Globalcode – Open4education
Exemplo em jQuery
$.ajax({ url: "/api/values", type: "GET", dataType: "json", success: function(data) { $(data).each(function(index, value){ console.log(index + ') '+ value); }); }});
![Page 11: Trilha .NET - REST na plataforma Microsoft com ASP.NET Web API](https://reader033.vdocument.in/reader033/viewer/2022061114/545c9c17af7959b90e8b4835/html5/thumbnails/11.jpg)
Globalcode – Open4education
Segurança no Web API
HTTP Basic Authentication
HTTP Digest Authentication
Forms/Windows Authentication
OAuth
![Page 12: Trilha .NET - REST na plataforma Microsoft com ASP.NET Web API](https://reader033.vdocument.in/reader033/viewer/2022061114/545c9c17af7959b90e8b4835/html5/thumbnails/12.jpg)
Globalcode – Open4education
HTTP Basic Authentication
![Page 13: Trilha .NET - REST na plataforma Microsoft com ASP.NET Web API](https://reader033.vdocument.in/reader033/viewer/2022061114/545c9c17af7959b90e8b4835/html5/thumbnails/13.jpg)
Globalcode – Open4education
Implementando a autenticação
Adicionar comportamento ao pipeline do ASP.NET Web API
A autenticação deve seguir os seguintes passosVerificar tipo de autenticação (Basic)
Converter de Base 64 para texto limpo
Extrair o usuário e senha
Verificar se o usuário e senha são válidos
Caso os dados estejam ok, então permite a requisição
Caso os dados não sejam informados ou estejam incorretos, então deve enviar a solicitação de autenticação:
WWW-Authenticate: Basic realm=“waldyrfelix.net”
![Page 14: Trilha .NET - REST na plataforma Microsoft com ASP.NET Web API](https://reader033.vdocument.in/reader033/viewer/2022061114/545c9c17af7959b90e8b4835/html5/thumbnails/14.jpg)
Globalcode – Open4education
Delegating Handler
Serve para fazer pequenas ações dentro do pipeline do ASP.NET Web API
É possível ter um Delegating Handler para fazer a autenticação HTTP Basic ou qualquer outra
Herdar a classe DelegatingHandler
Implementar o método SendAsync
Adicionar o handler nas configurações do Web API
![Page 15: Trilha .NET - REST na plataforma Microsoft com ASP.NET Web API](https://reader033.vdocument.in/reader033/viewer/2022061114/545c9c17af7959b90e8b4835/html5/thumbnails/15.jpg)
Globalcode – Open4education
Demo, demo, demo, demo…
https://gist.github.com/waldyrfelix/5988001
![Page 16: Trilha .NET - REST na plataforma Microsoft com ASP.NET Web API](https://reader033.vdocument.in/reader033/viewer/2022061114/545c9c17af7959b90e8b4835/html5/thumbnails/16.jpg)
Globalcode – Open4education
Exemplo em 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) { Console.WriteLine(s); }}https://gist.github.com/waldyrfelix/3983405
![Page 17: Trilha .NET - REST na plataforma Microsoft com ASP.NET Web API](https://reader033.vdocument.in/reader033/viewer/2022061114/545c9c17af7959b90e8b4835/html5/thumbnails/17.jpg)
Globalcode – Open4education
Exemplo em jQuery
$.ajax({ url: "/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); }); }});
https://gist.github.com/waldyrfelix/3983411
![Page 18: Trilha .NET - REST na plataforma Microsoft com ASP.NET Web API](https://reader033.vdocument.in/reader033/viewer/2022061114/545c9c17af7959b90e8b4835/html5/thumbnails/18.jpg)
Globalcode – Open4education
Dúvidas?