WebService中的用户验证问题

cuick2000 2002-06-21 02:28:12
比如我做一个WebService,但是我只希望特定的用户使用。所以加一个验证WebMethod,为login(string username,string pwd).这样我只有在验证通过后才能调用其他WebMethod。怎样可以实现???
...全文
283 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
cuick2000 2002-06-25
  • 打赏
  • 举报
回复
谢谢!!!
================================================================

百芳园里欲独艳,
万艳丛中却自芳。
cuick2000 2002-06-24
  • 打赏
  • 举报
回复
关于SOAPHeader的文章!!!谁有??
hydnoahark 2002-06-24
  • 打赏
  • 举报
回复
在QuickStart中有介绍:
You use the same technique to secure your XML Web services using Windows authentication that you used for .aspx pages

<configuration>

<system.web>
<authentication mode="Windows"/>
</system.web>

<location path="secureservice.asmx">

<system.web>
<authorization>
<allow users="Administrator"/>
<allow users="DOMAIN\Bradley"/>
<deny roles="BUILTIN\Power Users"/>
</authorization>
</system.web>

</location>

</configuration>

下面是一个使用SOAPHeader的例子:
soapheaders.aspx
<%@ Import Namespace="SoapHeaders" %>

<script language="C#" runat="server">

public void Page_Load(Object sender, EventArgs e) {

Response.Write("<h4><font face=\"verdana\">Using Soap Headers for Custom Authentication</font></h4>");

// Create a new instance of the UsingSoapHeaders
// proxy class used to call the remote .asmx file
HeaderService h = new HeaderService();

// Call the secure method without credentials
Response.Write("First call result: <p>");
try {
Response.Write(h.SecureMethod() + "<p>");
}
catch (Exception ex) {
Response.Write("<pre>" + ex.StackTrace + "</pre><p>");
}

// Create a new instance of the AuthHeader class
AuthHeaderCS myHeader = new AuthHeaderCS();

// Set the value of myHeader
myHeader.Username = "JohnDoe";
myHeader.Password = "password";

// Set the AuthHeader public member of the
// UsingSoapHeaders class to myHeader
h.AuthHeaderCSValue = myHeader;

// Call the secure method with credentials
Response.Write("Second call result: <p><pre>" + h.SecureMethod() + "</pre>");
}

</script>


soapheaders.asmx:

<%@ WebService Language="C#" Class="SoapHeadersCS.HeaderService" %>

using System;
using System.Web.Services;
using System.Web.Services.Protocols;

// Note the namespace has to be different from the one used
// on the proxy dll or we get errors about AuthHeader being
// defined in multiple places.
namespace SoapHeadersCS {

// AuthHeader class extends from SoapHeader
public class AuthHeaderCS : SoapHeader {
public string Username;
public string Password;
}

[WebService(Description="Simple sample to demonstrate use of SOAP Headers")]
public class HeaderService {

public AuthHeaderCS sHeader;

[WebMethod(Description="This method requires a custom soap header set by the caller")]
[SoapHeader("sHeader")]
public string SecureMethod() {

if (sHeader == null)
return "ERROR: Please supply credentials";

string usr = sHeader.Username;
string pwd = sHeader.Password;

if (AuthenticateUser(usr, pwd)) {
return "SUCCESS: " + usr + "," + pwd;
}
else {
return "ERROR: Could not authenticate";
}
}

private bool AuthenticateUser(string usr, string pwd) {

if ((usr != null)&&(pwd != null)) {
// could query a database here for credentials...
return true;
}
return false;
}
}
}


shanhe 2002-06-22
  • 打赏
  • 举报
回复
实际上都是采用类session的做法拉。我的做法是利用用户名、密码des算法产生一个字符串,相当于一个令牌,以后的调用采用此令牌来证明身份区分各个客户端。
用SOAPHeader可能会更好一点,不过有些麻烦。
qqchen79 2002-06-22
  • 打赏
  • 举报
回复
如果不复杂,安全性也不是特别重要,可以在在登陆(login)后为每个用户分配一个唯一的ID,此后的所有方法调用都必须包含这个id为参数。
更好些的设计可以用SOAPHeader。
wildhorse01 2002-06-22
  • 打赏
  • 举报
回复
我知道的方法只有每个WEBSERVICE方法前加上认证模块,不过,这种方法太落后

我猜测可能用SOAPHeader肯定会更好。
================================================================

CSDN 论坛助手 Ver 1.0 B0402提供下载。 改进了很多,功能完备!

★ 浏览帖子速度极快![建议系统使用ie5.5以上]。 ★ 多种帖子实现界面。
★ 保存帖子到本地[html格式]★ 监视您关注帖子的回复更新。
★ 可以直接发贴、回复帖子★ 采用XML接口,可以一次性显示4页帖子,同时支持自定义每次显示帖子数量。可以浏览历史记录!
★ 支持在线检测程序升级情况,可及时获得程序更新的信息。

★★ 签名 ●
可以在您的每个帖子的后面自动加上一个自己设计的签名哟。

Http://www.ChinaOK.net/csdn/csdn.zip
Http://www.ChinaOK.net/csdn/csdn.rar
Http://www.ChinaOK.net/csdn/csdn.exe [自解压]

wtwxx 2002-06-21
  • 打赏
  • 举报
回复
up
when 2002-06-21
  • 打赏
  • 举报
回复
关注
cuick2000 2002-06-21
  • 打赏
  • 举报
回复
在哪里?贴出来
crazysa 2002-06-21
  • 打赏
  • 举报
回复
我见到过一篇关于通过http basic auth和ssl来进行验证的文章
cuick2000 2002-06-21
  • 打赏
  • 举报
回复
我本来想保存通过验证用户的ip来判断是否合法,但存在很多问题。看来只好每次调用时都加以验证了。或者验证通过后,发送一个认证字符串给客户端,然后其他的WebMethod的参数中要有认证字符串变量。
toshin 2002-06-21
  • 打赏
  • 举报
回复
三层结构中无论是corba,ejb还是com+都很容易实现的,但是到了这里就很难了,不知道如何通过web service把一个object传递给客户端。因为web service是基于http的,而且是无状态的

12,162

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 Web Services
社区管理员
  • Web Services社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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