Xpode.com        Click here to Print this article.

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);






Contributed by:
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.

Resourse address on xpode.com
http://www.xpode.com/Print.aspx?Articleid=181

Click here to go on website