Basic Authentication in WebService Rest C #

1

Good afternoon !!! I'm implementing a WebService Rest but I'm not able to call the BasicAuthentication method from another class.

 [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "ConsultarCodigo/{cpfcnpj}")]
    public ConsultaPessoa GetPessoa(string cpfcnpj)
    {
        try
        {
            ConsultaPessoa consultaPessoa = new ConsultaPessoa();

            using (var conn = Connection.Conn)
            {
                IDbCommand comando = conn.CreateCommand();
                comando.CommandText = @"SELECT A.HANDLE,
                                               A.NOME
                                          FROM MS_PESSOA A
                                         WHERE A.CNPJCPFSEMMASCARA = '" + cpfcnpj + "'";

                using (IDataReader reader = comando.ExecuteReader())
                {
                    if (!reader.Read())
                    {
                        throw new Exception("Pessoa não encontrada");
                    } 
                    consultaPessoa.codigo = Convert.ToInt32(reader.GetValue(0));
                    consultaPessoa.nome = reader.GetString(1);
                }

                return consultaPessoa;
            }

        }
        catch (Exception ex)
        {
            throw new WebFaultException<string>(ex.Message, System.Net.HttpStatusCode.BadRequest);
        }
    }

I have tried to add the class in the call where the WebService URI is made available even though it has not been successful, I have already researched a number of other websites and I have not found anyone with this problem. Here is an example of how I'm calling together in the URI.

[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "ConsultarCodigo/{cpfcnpj}"), BasicAuthenticationAttribute]

However, it does not enter the method for performing user authentication.

Authentication class.

public class BasicAuthenticationAttribute : AuthorizationFilterAttribute
{
    public override void OnAuthorization(System.Web.Http.Controllers.HttpActionContext actionContext)
    {
        if (actionContext.Request.Headers.Authorization == null)
        {
            actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized);
        }
        else
        {
            // Gets header parameters  
            string authenticationString = actionContext.Request.Headers.Authorization.Parameter;
            string originalString = Encoding.UTF8.GetString(Convert.FromBase64String(authenticationString));

            // Gets username and password  
            string username = originalString.Split(':')[0];
            string password = originalString.Split(':')[1];

            // Validate username and password  
            if (!ApiSecurity.VaidateUser(username, password))
            {
                // returns unauthorized error  
                actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized);
            }
        }

        base.OnAuthorization(actionContext);
    }
}

Has anyone caught this problem before or know how to solve it? Thank you!

    
asked by anonymous 16.11.2017 / 20:12

1 answer

0

You have missed decorating your method with the attribute you created.

[BasicAuthentication]
public ConsultaPessoa GetPessoa(string cpfcnpj) { ... }
    
17.11.2017 / 09:42