Guest User

Untitled

a guest
Nov 16th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "ConsultarCodigo/{cpfcnpj}")]
  2. public ConsultaPessoa GetPessoa(string cpfcnpj)
  3. {
  4. try
  5. {
  6. ConsultaPessoa consultaPessoa = new ConsultaPessoa();
  7.  
  8. using (var conn = Connection.Conn)
  9. {
  10. IDbCommand comando = conn.CreateCommand();
  11. comando.CommandText = @"SELECT A.HANDLE,
  12. A.NOME
  13. FROM MS_PESSOA A
  14. WHERE A.CNPJCPFSEMMASCARA = '" + cpfcnpj + "'";
  15.  
  16. using (IDataReader reader = comando.ExecuteReader())
  17. {
  18. if (!reader.Read())
  19. {
  20. throw new Exception("Pessoa não encontrada");
  21. }
  22. consultaPessoa.codigo = Convert.ToInt32(reader.GetValue(0));
  23. consultaPessoa.nome = reader.GetString(1);
  24. }
  25.  
  26. return consultaPessoa;
  27. }
  28.  
  29. }
  30. catch (Exception ex)
  31. {
  32. throw new WebFaultException<string>(ex.Message, System.Net.HttpStatusCode.BadRequest);
  33. }
  34. }
  35.  
  36. [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "ConsultarCodigo/{cpfcnpj}"), BasicAuthenticationAttribute]
  37.  
  38. public class BasicAuthenticationAttribute : AuthorizationFilterAttribute
  39. {
  40. public override void OnAuthorization(System.Web.Http.Controllers.HttpActionContext actionContext)
  41. {
  42. if (actionContext.Request.Headers.Authorization == null)
  43. {
  44. actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized);
  45. }
  46. else
  47. {
  48. // Gets header parameters
  49. string authenticationString = actionContext.Request.Headers.Authorization.Parameter;
  50. string originalString = Encoding.UTF8.GetString(Convert.FromBase64String(authenticationString));
  51.  
  52. // Gets username and password
  53. string username = originalString.Split(':')[0];
  54. string password = originalString.Split(':')[1];
  55.  
  56. // Validate username and password
  57. if (!ApiSecurity.VaidateUser(username, password))
  58. {
  59. // returns unauthorized error
  60. actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized);
  61. }
  62. }
  63.  
  64. base.OnAuthorization(actionContext);
  65. }
  66. }
Add Comment
Please, Sign In to add comment