Basic Authentication in Webservice Rest C#

Asked

Viewed 544 times

1

good afternoon!! I’m implementing a Webservice Rest but I’m not able to call the Basicauthentication method 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 already tried to add the class in the call where the Webservice URI is made available but without success, I have already searched in several websites but I have not found anyone with this problem. Below is an example of how I am calling together in the URI.

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

But even so it does not enter the method to perform 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 picked up this problem before or know how to fix it? This already I thank!

  • files are in the same project? Are in the same namespace? abs.

  • Yes, the files are in the same project

1 answer

0


It lacked to decorate its method with the created attribute.

[BasicAuthentication]
public ConsultaPessoa GetPessoa(string cpfcnpj) { ... }
  • I have already memorized the method with my attribute, but it does not enter this attribute.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.