[英]Consuming web service SOAP Header
我正在嘗試學習Web服務並創建了一個簡單的服務。 現在,我想通過身份驗證來保護它。 我發現人們通過從SoapHeader派生一個類來實現這一目標。 我實現了它,但是現在我的網絡方法還需要一個參數。 我認為我做錯了,因為在其他示例中,人們沒有將其身份驗證值作為參數發布。
這是我的網絡服務代碼。
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";
}
}
這是我簡單易用的控制台應用程序。
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();
}
在另一個例子中,人們曾經有這條線
webService.AuthHeaderValue = authentication;
但是在我的代碼中,我找不到AuthUserValue,並且如果可能的話,我不想將那些身份驗證值作為參數傳遞給WebMethod。 這是唯一的方法還是我錯過了什么?
謝謝。
我自己找到了解決方案。
ServiceReference1.WebServiceCCMSoapClient ws = new
ServiceReference1.WebServiceCCMSoapClient();
需要使用ServiceReference1.ActualServiceName。 這樣我可以使用ServiceNameValue。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.