Click here to hide categories Click here to show left categories

User: Home          welcome : Guest          Log In / Register here     




How to Authenticate Web Service

CREATE A WEB SERVICE

                public class AuthenticationHeader : SoapHeader

{

    public string Username;

    public string Password;

}

 

[WebService(Namespace = "http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class MyWebService : System.Web.Services.WebService

{

 

    public AuthenticationHeader AuthHeader;

 

    [WebMethod(), SoapHeader("AuthHeader")]

    public string GetMessage()

    {

        if (AuthHeader.Username == "user" && AuthHeader.Password == "password")

            return "This is the secret message!!!";

        else

            return "YOU ARE NOT AUTHORIZED TO SEE THIS MESSAGE!";

    }

 

}

 

                CONSUME THE WEB SERVICE

        MyWebService proxy = newMyWebService();

        AuthenticationHeader authInfo = newAuthenticationHeader();

        authInfo.Username = "user";

        authInfo.Password = "password";

        proxy.AuthHeader = authInfo;

        string result = proxy.GetMessage();

        Response.Write(result);

Share this article   |    Print    |    Article read by 2294 times
Author:
Guest
Guest user is that user who have done some activity on website without login. Activity contains commenting on any article, uploading an image on website etc.
Related Articles:
Related Interview Questions: