How can I test authenticated webservice?

7

Hello, I have an authentication service already mounted in C # and wanted to do any testing with it. It can be some webform page, which returns a positive or negative, or a C # class even though it runs on console and does the same. Already have all structure mounted, just consume, I already have a password in web.config with md5. is using the SOAP service.

namespace Test.Services
{
/// <summary>
/// Summary description for TestService
/// </summary>
[WebService(Namespace = "http://www.este.com.br", Description = "Servico de interface com o sistema de gerenciamento eletronico de documentos")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class TestService : System.Web.Services.WebService
{

    [WebMethod(Description="Metodo que adiciona um arquivo a base do Test")]
    public void  AddArquivo(Autenticacao aut, Arquivo arq)
    {
        Autenticar(aut);
        ArquivoController.GetInstance().AddArquivo(arq);
    }        

    [WebMethod]
    public Arquivo GetArquivo(Autenticacao autenticacao)
    {
        return null;
    }        


    /// <summary>
    /// Método que executa a autenticação dos utilizadores do webservices
    /// </summary>
    /// <param name="aut">Objeto contendo a autenticação</param>
    private void Autenticar(Autenticacao aut)
    {
        if (string.Compare(aut.Usuario, Util.Util.WebConfigurations.GetValue("usuariowebservice")) != 0 || string.Compare(Util.Util.GUIDs.GetKeyMD5(aut.Senha), Util.Util.WebConfigurations.GetValue("senhawebservice")) != 0)
            throw new TestServiceException("Usuário e/ou senha inválido(s)");
    }
}

/// <summary>
/// Classe que do objeto de autenticação dos usuários do serviço
/// </summary>
public class Autenticacao
{
    public string Usuario { get; set; }
    public string Senha { get; set; }
}
}

How to create a test class, to try to authenticate with any password? just test.

    
asked by anonymous 03.02.2015 / 02:49

1 answer

2

Friend, you can use SoapUI Then add the address of your webservice, which should be something of the genre: link

Just a hint, for new webservices, I advise you to use WCF, as in the example below ... start a new project by using WCF Service Application. In this project, you will have an interface and a class, called respectively IService and Service. Its interface would be something of the genre:

[ServiceContract(Namespace="http://www.este.com.br")]
public interface IService1
{
    [OperationContract(IsOneWay = true)]
    void AddArquivo(Autenticacao aut, Arquivo arq);

    [OperationContract]
    Arquivo GetArquivo(Autenticacao autenticacao);
}

[DataContract]
public class Autenticacao
{
    [DataMember]
    public string Usuario { get; set; }
    [DataMember]
    public string Senha { get; set; }
}

[DataContract]
public class Arquivo
{

}

And your class something of the genre:

public class Service1 : IService1
{
    public void AddArquivo(Autenticacao aut, Arquivo arq)
    {
        Autenticar(aut);
        ArquivoController.GetInstance().AddArquivo(arq);
    }

    public Arquivo GetArquivo(Autenticacao autenticacao)
    {
        return null;
    }

    private void Autenticar(Autenticacao aut)
    {
        if (string.Compare(aut.Usuario, Util.Util.WebConfigurations.GetValue("usuariowebservice")) != 0 || string.Compare(Util.Util.GUIDs.GetKeyMD5(aut.Senha), Util.Util.WebConfigurations.GetValue("senhawebservice")) != 0)
            throw new TestServiceException("Usuário e/ou senha inválido(s)");
    }
}

To test, just give a "Play" while the Service Class is open.

    
03.02.2015 / 12:07