※※※※自定义 SOAP头 身份验证问题(郁闷)。※※※※※

曲滨_銘龘鶽 曲滨_銘龘鶽 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
自定义 SOAP头
public class SelfSoapHeader : SoapHeader
{
public string UserNmae="";
public string Password="";
}
//一个简单的 WebService
public class Service1 : System.Web.Services.WebService
{
public SelfSoapHeader thisSoapHeader;
public Service1()
{
//CODEGEN: 该调用是 ASP.NET Web 服务设计器所必需的
InitializeComponent();
}

组件设计器生成的代码....


// WEB 服务示例
// HelloWorld() 示例服务返回字符串 Hello World
// 若要生成,请取消注释下列行,然后保存并生成项目
// 若要测试此 Web 服务,请按 F5 键

[WebMethod,SoapHeader("thisSoapHeader")]
public string UserLoad()
{

if( thisSoapHeader.UserNmae=="FlashElf" && thisSoapHeader.Password=="1234")
{

return "True";

}
else
{
this.Dispose();
throw new Exception("用户严验证个失败!");

}
}

}
[WebMethod,SoapHeader("thisSoapHeader")]
public string HelloWorldTow()
{

return "HelloWorldTow";
}

}
//Windows 客户端 myServer 是上面的 WebService
private void button1_Click(object sender, System.EventArgs e)
{
SelfSoapHeader soap=new SelfSoapHeader();
soap.Password=textBox2.Text;
soap.UserNmae=textBox1.Text;
myServer.SelfSoapHeaderValue=soap;
try
{
label1.Text= myServer.UserLoad();

}
catch(Exception Ex)
{

MessageBox.Show(Ex.Message);

}

}

private void button2_Click(object sender, System.EventArgs e)
{
try
{

label1.Text= myServer.HelloWorldTow();
}
catch(Exception Ex)
{
MessageBox.Show(Ex.ToString());

}

}
///////////////////////////////////////////////////
问题是 自定义 SOAP头 内的 密码 和 用户 不管是否正确都可以 WebService 下的其他函数。 调用函数
现在即使调用 WebService 的 UserLoad() 失败也可以调用
WebService 下的其他函数。
问一下
如果要写身份验证是否 WebService 下的每个函数都要象UserLoad 函数那样验证一次

有没有什么方式可以
一次验证不成功 WebService 下的每个函数都不可调用。
我不想用 Windows 验证。。。
...全文
评论 8
收藏 已收藏
分享
返回顶部
曲滨_銘龘鶽
Web Services 12,164 成员 16,322 内容