导读
最近遇到一个问题,暴露在网络上的Web Service如何做身份验证,而不被其他人说是用。
众所周知,WebService是为企业需求提供的在线应用服务,其他公司或应用软件能够通过Internet来访问并使用这项在线服务。但在有些时候的某些应用服务不希望被未授权访问,那么此时我们可以一下几种方法来实现身份验证。
解决方案
第一种方法:在WebServices中引入SoapHeader
WebServices
MySoapHeader.cs类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WS.WebServices
{
public class MySoapHeader:System.Web.Services.Protocols.SoapHeader
{
//WebServices访问用户名
public string UserName { get; set; }
//WebServices访问密码
public string Password { get; set; }
public MySoapHeader()
{
//在此处添加构造函数逻辑
}
public MySoapHeader(string uname, string upassword)
{
init(uname, upassword);
}
private void init(string uname, string upassword)
{
this.UserName = uname;
this.Password = upassword;
}
//验证用户是否有权访问内部接口
private bool isValid(string uname, string upassword, out string msg)
{
msg = "";
if (uname == "admin" && upassword == "123456")
{
return true;
}
else
{
msg = "对不起,你无权调用此WebServices";
return false;
}
}
//验证用户是否有权访问外部接口
public bool isValid(out string msg)
{
return isValid(this.UserName, this.Password, out msg);
}
}
}
WebService1.asmx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using WS.WebServices;
namespace WS.WebService
{
/// <summary>
/// WebService1 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
// [System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
public WebService1()
{
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
public MySoapHeader myheader;
[WebMethod]
public string HelloWorld() //普通WebServices,无需验证
{
return "Hello World";
}
[WebMethod]
[SoapHeader("myheader")]//加入此特性标签的WebService需要验证,不加则为普通WebService无需验证
public string GetTime()
{
string msg = "";
//验证是否有权访问
if (myheader.isValid(out msg) == false)
{
return msg;
}
//如果有权访问,就返回当前时间
return DateTime.Now.ToString();
}
}
}
客户端
然后再客户端WS.WebClient中添加对WebServices项目的服务引用,命名空间为SR
客户端中的WebForm1.aspx.cs后台类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WS.WebClient
{
using WS.WebServices;
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SR.WebService1SoapClient client=new SR.WebService1SoapClient();实例化一个WebServices服务的客户端代理类。
SR.MySoapHeader header = new SR.MySoapHeader();
header.UserName = "admin";
header.Password = "123456";
string time = client.GetTime(header); //将这个header实例传递到GetTime这个方法中
this.time.InnerText = time; //给这个time值赋给页面Id为time的服务器控件的InnerText属性
}
}
}
客户端中的WebForm1.aspx文件
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WS.WebClient.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div id="time" runat="server">
</div>
</form>
</body>
</html>