Click here to Skip to main content
15,917,793 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have implemented authentication but i get error as:
System.NullReferenceException: Object reference not set to an instance of an object.

What I have tried:

C#
namespace testwebapi
{
    /// <summary>
    /// Summary description for testwebapi
    /// </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 testwebapi: System.Web.Services.WebService
    {
        public class AuthHeader : SoapHeader
        {
            public string Username;
            public string Password;
        }

        public AuthHeader Authentication;


        [SoapHeader("Authentication", Required = true)]
        [WebMethod(Description = "WebMethod authentication testing")]
        public string SensitiveData()
        {

            //Do our authentication
            //this can be via a database or whatever
            if (Authentication.Username == ConfigurationManager.AppSettings["ff_username"] &&
                        Authentication.Password == ConfigurationManager.AppSettings["ff_password"])
            {
                //Do your thing
                string success = "";
                success = "{\"status\": \"test web api security.\"}";
                this.Context.Response.ContentType = "application/json; charset=utf-8";
                this.Context.Response.Write(success);
                return "";

            }
            else
            {
                //if authentication fails
                return null;
            }
        }
Posted
Updated 10-Jul-16 22:17pm
v2
Comments
Suvendu Shekhar Giri 11-Jul-16 4:17am    
any luck with debugging?
F-ES Sitecore 11-Jul-16 4:23am    
http://www.codeproject.com/search.aspx?q=Object+reference+not+set+to+an+instance+of+an+object&sbo=qa&usfc=false&x=8&y=3

Use the debugger to at least learn what line throws the error and what exactly is null.
Member 11466758 11-Jul-16 5:19am    
if (Authentication.Username == ConfigurationManager.AppSettings["ff_username"] &&
Authentication.Password == ConfigurationManager.AppSettings["ff_password"])
THIS LINE HAVE ERROR AS Object reference not set to an instance of an object.
F-ES Sitecore 11-Jul-16 5:38am    
So either "Authentication" is null, ConfigurationManager.AppSettings["ff_username"] is null, or ConfigurationManager.AppSettings["ff_password"] is null. Which of those being null dictates what the problem is and what the ultimate solution is.

As said above, use the debugger. We can't debug your code and inspect your variables remotely.
Member 11466758 11-Jul-16 7:09am    
this value is null "Authentication" while debugging

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