[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

soapheader is null in webservice

Posted on 2013-02-06
1
Medium Priority
?
1,679 Views
Last Modified: 2013-02-07
When I call my webservice and authenticate user, an authorisation token is set and I can see the token value while debugging. After the token is set when I try to call a method, the method checks for that token but the value for that token is null.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
using System.Data;
using HPS.Core;
using HPS.Thesaurus.BusinessObject;
using HPS.Thesaurus.BusinessObject.Collections;
using System.Web.Script.Services;
using System.Web.Services.Protocols;
namespace TaxonomyWebService
{

    /// <summary>
    /// Soap Header for the Secured Web Service.
    /// Username and Password are required for AuthenticateUser(),
    ///   and AuthenticatedToken is required for everything else.
    /// </summary>
    public class TaxonomyWebServiceHeader : System.Web.Services.Protocols.SoapHeader
    {
        public string Username;
        public string Password;
        public string AuthenticatedToken;
    }
    
    /// <summary>
    /// Summary description for NESTaxonomyWS
    /// </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 NESTaxonomyWS : System.Web.Services.WebService
    {
        public TaxonomyWebServiceHeader SoapHeader;

        [WebMethod]
        [SoapHeader("SoapHeader")]
        public string AuthenticateUser()
        {
            if (SoapHeader == null)
                return "Please provide a Username and Password";
            if (string.IsNullOrEmpty(SoapHeader.Username) || string.IsNullOrEmpty(SoapHeader.Password))
                return "Please provide a Username and Password";
            // Are the credentials valid?
            if (!IsUserValid(SoapHeader.Username, SoapHeader.Password))
                return "Invalid Username or Password";
            // Create and store the AuthenticatedToken before returning it
            string token = Guid.NewGuid().ToString();
            HttpRuntime.Cache.Add(token,SoapHeader.Username,null,System.Web.Caching.Cache.NoAbsoluteExpiration,TimeSpan.FromMinutes(60), System.Web.Caching.CacheItemPriority.NotRemovable,null);
            return token;
        }

        private bool IsUserValid(string Username, string Password)
        {
            // TODO: Implement Authentication

            
            return true;
        }

        private bool IsUserValid(TaxonomyWebServiceHeader SoapHeader)
        {
            if (SoapHeader == null)
                return false;
            return true;
            // Does the token exists in our Cache?
            //if (!string.IsNullOrEmpty(SoapHeader.AuthenticatedToken))
            //    return (HttpRuntime.Cache[SoapHeader.AuthenticatedToken] != null);
            //return false;
        }




        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        [SoapHeader("SoapHeader")]
        public List<Term> GetTopLevelTerms()
        {
            if (IsUserValid(SoapHeader))
            {
                List<Term> tl = new List<Term>();
                DataTable dt = new DataTable();
                string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ThesaurusConnectionString"].ConnectionString;

                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    connection.Open();
                    SqlCommand cmd = new SqlCommand("Term_GetTopLevelTerm", connection);
                    cmd.CommandType = CommandType.StoredProcedure;

                    SqlDataAdapter da = new SqlDataAdapter(cmd);

                    da.Fill(dt);

                    if (dt.Rows.Count > 0)
                    {
                        int count = 0;
                        foreach (DataRow dr in dt.Rows)
                        {

                            tl.Add(FillData(dr));

                            count++;
                        }
                    }

                }


                return tl;
            }
            else
            {

                return null;

            }
        }
        }

Open in new window


The web service client

 protected void btnOk_Click(object sender, EventArgs e)
    {
        NESTaxonomyWSRef.NESTaxonomyWSSoapClient Obj = new NESTaxonomyWSRef.NESTaxonomyWSSoapClient();

        NESTaxonomyWSRef.TaxonomyWebServiceHeader headerObj= new NESTaxonomyWSRef.TaxonomyWebServiceHeader();

        headerObj.Username="hello";
        headerObj.Password="user";



       var v1=Obj.AuthenticateUser(headerObj);
       headerObj.AuthenticatedToken = v1.ToString();
       var v= Obj.GetTopLevelTerms();

       
        
    }

Open in new window

0
Comment
Question by:mmalik15
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 16

Accepted Solution

by:
Rose Babu earned 2000 total points
ID: 38859930
Here is an updated working code as you wanted. also update IsUserValid function with the necessary checking.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;

namespace WebService_CSHARP
{    
    /// <summary>
    /// Soap Header for the Secured Web Service.
    /// Username and Password are required for AuthenticateUser(),
    ///   and AuthenticatedToken is required for everything else.
    /// </summary>
    public class TaxonomyWebServiceHeader : System.Web.Services.Protocols.SoapHeader
    {
        public string Username;
        public string Password;
        public string AuthenticatedToken;
    }

    /// <summary>
    /// Summary description for EE_TaxonomyService
    /// </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 EE_TaxonomyService : System.Web.Services.WebService
    {
        public TaxonomyWebServiceHeader SoapHeader;
        
        private bool IsUserValid(string Username, string Password)
        {
            // TODO: Implement Authentication
			// Update this function 
            if (Username == "test" && Password == "test")
            {
                return true;
            }
            else
            {
                return false;
            }            
        }

        private bool IsUserValid(TaxonomyWebServiceHeader SoapHeader)
        {
            if (SoapHeader == null)
                return false;
            return true;
            // Does the token exists in our Cache?
            //if (!string.IsNullOrEmpty(SoapHeader.AuthenticatedToken))
            //    return (HttpRuntime.Cache[SoapHeader.AuthenticatedToken] != null);
            //return false;
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        [SoapHeader("SoapHeader")]
        public string GetToken()
        {
            if (SoapHeader == null)
                return "Please provide a Username and Password";
            if (string.IsNullOrEmpty(SoapHeader.Username) || string.IsNullOrEmpty(SoapHeader.Password))
                return "Please provide a Username and Password";
            // Are the credentials valid?
            if (!IsUserValid(SoapHeader.Username, SoapHeader.Password))
                return "Invalid Username or Password";
            



            // Create and store the AuthenticatedToken before returning it
            string token = Guid.NewGuid().ToString();
            HttpRuntime.Cache.Add(token, SoapHeader.Username, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(60), System.Web.Caching.CacheItemPriority.NotRemovable, null);
            
            return token;            
        }


    }
}

Open in new window

i used test authentication. use test as username and test as password for this sample code. This works for me.

Just try this.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
New Relic recently released its Synthetics product that allows for the creation of performance monitors that periodically test a site's performance. If you wish to test an interactive workflow New Relic employs Selenium WebDriverJS to run those test…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question