Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am implementing soap header in asp.net for web api authentication i got error as
System.NullReferenceException: Object reference not set to an instance of an object
where i have to initiate the variable like username and password.

What I have tried:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

namespace testapp
{
    /// <summary>
    /// Summary description for Service
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service : System.Web.Services.WebService
    {
    public AuthSoapHd spAuthenticationHeader;

    public Service () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod, SoapHeader("spAuthenticationHeader")]
    public string HelloWorld() 
    {
        if (spAuthenticationHeader.strUserName == "TestUser" && spAuthenticationHeader.strPassword == "TestPassword")
        {
            return "User Name : " + spAuthenticationHeader.strUserName + " and " + "Password : " + spAuthenticationHeader.strPassword;
        }
        else
        {
            return "Access Denied";
        }
    }

    public class AuthSoapHd : SoapHeader
    {
        public string strUserName;
        public string strPassword;
    }

    }
}
Posted
Updated 8-Aug-16 21:37pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900