关于web service的用户验证的问题。

binapex 2006-09-06 10:56:28
System.Web.Services.Protocols.SoapException: 服务器无法处理请求。 ---> System.NullReferenceException: 未将对象引用设置到对象的实例。
这是错误信息

代码详见:http://community.csdn.net/Expert/topic/5001/5001706.xml?temp=.615307
...全文
177 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
scow 2006-09-07
  • 打赏
  • 举报
回复
看看header是在哪实例化的,如果header在click事件之前保证实例化了,你这样写可以调,没问题
scow 2006-09-07
  • 打赏
  • 举报
回复
header在click事件之外定义的,回调之后就为null了
binapex 2006-09-06
  • 打赏
  • 举报
回复
我是进行了验证啊,可是怎么验证呢?我在每个应用方法里面都有像AplicationAMethod里面那样写的
scow 2006-09-06
  • 打赏
  • 举报
回复
这种验证方式是基于消息的,即每次调用webservice方法都要验证,而不是验证一次就行了
binapex 2006-09-06
  • 打赏
  • 举报
回复
怎么每人回答我?
代码如下:
public class Authentication : System.Web.Services.Protocols.SoapHeader
{
public string _Username;
public string _Password;

public string UserName
{
get{return _Username;}
set{_Username=value;}
}
public string Password
{
get{return _Password;}
set{_Password=value;}
}

public bool ValidUser(string in_Username,string in_Password)
{
……
}
}
在web service文件中是这样定义的;
public Authentication header;

[WebMethod(EnableSession = true)]
[System.Web.Services.Protocols.SoapHeader("header")]
public void AMethod()
{
if (header.ValidUser(header.UserName, header.Password))
{
……
}
}

[WebMethod(EnableSession = true)]
[System.Web.Services.Protocols.SoapHeader("header")]
public void BMethod()
{
if (header.ValidUser(header.UserName, header.Password))
{
……
}
}

我在调用这个web serivce时(在asp.net),
private local.servicename service1= new local.servicename();
private local.Authentication header = new local.Authentication();

我在AplicationAMethod里
header.UserName = "aa";
header.Password = "bb";
service1.AuthenticationValue = header;
service1.Amethod();
在这里可以正常运行。
但是在同一个页面里,当我进行下一步操作,也就是在另外一个方法里再次调用Bmethod时,就会提示验证错误,跟踪进去可以看到header为null。
binapex 2006-09-06
  • 打赏
  • 举报
回复
高手快来啊,急等
binapex 2006-09-06
  • 打赏
  • 举报
回复
to zhongwanli(㊣【为了老婆,二次重构____然后升★★】㊣) :
不太明白你的意思,能不能讲清楚一点?
是不是因为这里[WebMethod(EnableSession = true)]为true?
如果这里EnableSession = false呢?
zhongwanli 2006-09-06
  • 打赏
  • 举报
回复
EnableSession 了

也得用Session 将他保存吧。
binapex 2006-09-06
  • 打赏
  • 举报
回复
是这样的,我有两个按钮,我先执行其中一个:
private void method1()
{
service1.AuthenticationValue = header;
service1.Amethod();
}
然后我单击另外一个按钮,执行第二个方法:
private void method2()
{
service1.AuthenticationValue = header;
service1.Bmethod();
}

就这样,我单击第一个按钮执行的结果是正确的,不会产生错误。
而当我单击第二个按钮执行method2方法的时候,程序就会报SOAP的相关错误,也就是上面我提过的错误报告。
binapex 2006-09-06
  • 打赏
  • 举报
回复
不会每次都要重新定义吧?
scow 2006-09-06
  • 打赏
  • 举报
回复
//这样可以
service1.AuthenticationValue = header;
service1.Amethod();
service1.Bmethod();

//这样也行
service1.AuthenticationValue = header;
service1.Amethod();
service2.AuthenticationValue = header;
service2.Bmethod();

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧