简体   繁体   中英

Consuming web service SOAP Header

I am trying to learn web services and created a simple one. Now, I want to secure it with authentication. I found out people achieve this with deriving a class from SoapHeader. I achieved it but now my webmethod requires one more parameter. I am thinking that I did something wrong because in other examples, people do not post their authentication values as parameter.

Here is my webservice code.

    public class AuthUser : SoapHeader
    {
        public string Uname;
        public string Pwd;
    }
    public AuthUser au;

    string cs = ConfigurationManager.ConnectionStrings["ModelCCM"].ConnectionString;
    [WebMethod]
    [SoapHeader("au", Direction = SoapHeaderDirection.In)]
    public string InsertCustomer(string Name, string Phone, string City, string Email)
    {
        if (au.Uname == "****" && au.Pwd == "*****")
        {
            int getCustomerId;
            using (SqlConnection con = new SqlConnection(cs))
            {
                using (SqlCommand cmd = new SqlCommand("InsertCustomer", con))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Name", Name);
                    cmd.Parameters.AddWithValue("@Phone", Phone);
                    cmd.Parameters.AddWithValue("@City", City);
                    cmd.Parameters.AddWithValue("@Email", Email);
                    cmd.Connection = con;
                    con.Open();
                    getCustomerId = Convert.ToInt32(cmd.ExecuteScalar());
                    con.Close();
                    return getCustomerId.ToString();
                }
            }
        }
        else
        {
            return "Unauthorized Attempt";
        }
    }

And here is my simple consuming console application.

       static void Main(string[] args)
    {
        ServiceReference1.WebServiceCCMSoapClient ws = new ServiceReference1.WebServiceCCMSoapClient();
        ServiceReference1.AuthUser authUser = new ServiceReference1.AuthUser();
        authUser.Uname = "****";
        authUser.Pwd = "******";     
        string result = ws.InsertCustomer(authUser,"test", "test", "test", "test"); // here I need to pass authUser as parameter
        Console.WriteLine(result);
        Console.ReadLine();
    } 

In another examples, people used to have this line

   webService.AuthHeaderValue = authentication;

But in my code I can't find AuthUserValue and if possible I don't want to pass those authentication values as parameter to WebMethod. Is this the only way for or am I missing something ?

Thanks.

I found out the solution by myself.Instead of using,

    ServiceReference1.WebServiceCCMSoapClient ws = new 
    ServiceReference1.WebServiceCCMSoapClient();

need to use, ServiceReference1.ActualServiceName. This way I can use ServiceNameValue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM