C# 如何使用用户名和密码作为凭据创建asmx web服务?
我想创建Web服务,将用户名和密码作为凭据 我的班级是C# 如何使用用户名和密码作为凭据创建asmx web服务?,c#,asp.net,web-services,asmx,C#,Asp.net,Web Services,Asmx,我想创建Web服务,将用户名和密码作为凭据 我的班级是 public class UserDetails : System.Web.Services.Protocols.SoapHeader { public string userName { get; set; } public string password { get; set; } public bool IsValid() { //Write
public class UserDetails : System.Web.Services.Protocols.SoapHeader
{
public string userName { get; set; }
public string password { get; set; }
public bool IsValid()
{
//Write the logic to Check the User Details From DataBase
//i can chek with some hardcode details UserName=Nitin and Password=Pandit
return this.userName == "xxxxx" && this.password == "xxxxxxx";
//it'll check the details and will return true or false
}
我的asmx文件是
public UserDetails User;
[WebMethod]
[SoapHeader("User", Required = true)]
public void SayHello(string userName)
{
if (User != null)
{
if (User.IsValid())
{
Context.Response.ContentType = "application/json; charset=utf-8";
Context.Response.Write("Hello... " + userName + "");
}
else
{
Context.Response.ContentType = "application/json; charset=utf-8";
Context.Response.Write("user crediantials not valid");
}
}
else
{
Context.Response.ContentType = "application/json; charset=utf-8";
Context.Response.Write("Error in authentication");
}
}
当我打电话给服务人员时,服务人员不会接受其他情况下的急诊。
iam调用如下所示的服务,但收到错误响应
HttpWebRequest request = HttpWebRequest.Create(Url.weburl + "SayHello?userName=pavan") as HttpWebRequest;
// String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
// request.Credentials = new NetworkCredential("reload", "reload123");
request.Method = "GET";
WebResponse response = await request.GetResponseAsync();
using (var reader = new StreamReader(response.GetResponseStream()))
{
result = reader.ReadToEnd();
}
无论何时调用服务,用户值始终为空。如何传递凭据。请帮助我。我最近两天一直在搜索此内容。仍然没有找到解决方案,请帮助我。查看。这是我正在使用的代码,请看一看。这就是我正在使用的代码