In this article we will learn how to use a Web Method after Authentication.
If we want to secure our web method from an unauthenticated client request then there are many ways to do this but there is also a way to create a web service and create all the web methods for Authentication first so we can do that with a custom SOAP header.We embed the SOAP header into our message and validate its contents on the server.If the SOAP header validates successfully then the web server sends the web service response to the client application.We need to use [SoapHeader] on every [WebMethod] and for this attribute we must use a namespace “using System.Web.Services.Protocols;”.So let's have an example.Step 1Open Visual Studio then select File -> New -> Web site.Step 2Add a Web Service File to the web site.Provide the name to the Web Service File that will add a .asmx file to the web site project.Then delete the existing class file that is provided by the web service template.And add a new Class File to create [WebMethod] and [WebService].With a specified class name.Step 3Now use the namespace first that is required.And create any Test [WebMethod].Step 4Now edit and set the CodeBehind and class the property in the .asmx file with the name of the web service class.Step 5Now right-click on your Web Service (.asmx) file and view in it in a browser to test [WebMethod].Then you will see the name of your web methods list on the page like.Click the name of Method to test.Enter the UserName Parameter's value like “Nitin Pandit”. Now click on the Invoke button to see the result of your web method.Then the result will show on the page in XML format.Step 6The methods are working perfectly but I need to define the Authentication before calling every [WebMethod]. So add a class file to create user credentials.I added a class to my web service with UserDetails and I also declared the IsValid() function that returns a bool value after checking that the user details are vailed for login or not to be authenticated.Code
Hands on ASP.NET GridView