How can I test authenticated webservice?

Asked

Viewed 1,637 times

7

Hello, I have an authentication service already mounted in c# and wanted to do any test with it. It can be a webform page, which returns a positive or negative, or a class c# even if it runs per console and does the same. Already has all the structure mounted, just consume, I already have a password on web.config with md5. is using SOAP the 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? only test.

  • I just wanted to draw attention to one point. (...)já tenho uma senha no web.config com md5(...) I don’t know why, but if it’s for security reasons, it’s a wasted effort.

1 answer

2


Friend, for this you can use the Soapui Then add the address of your webservice, which should be something of the genre: http://localhost:xxxxx/testeservice.asmx

Just a tip, for new webservices, I advise you to use WCF, as in the example below... start a new project 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 like that:

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" being with the Service Class open.

  • can you give me an example in code?

  • Buddy, Soapui is just a program, so you don’t need to implement any code, as for WCF, I added an example in the answer.

Browser other questions tagged

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