当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.NET中Webservice安全 实现访问权限控制

ASP.NET中Webservice安全 实现访问权限控制

2017年12月12日  | 移动技术网IT编程  | 我要评论

和尚尼姑偷腥图片图,太阳传奇txt全集下载,猎场10集

一、 概述:

  web services是由企业发布的完成其特定商务需求的在线应用服务,其他公司或应用软件能够通过internet来访问并使用这项在线服务。它逻辑性的为 其他应用程序提供数据与服务.各应用程序通过网络协议和规定的一些标准数据格式(http,xml,soap)来访问web service,通过web service内部执行得到所需结果。由于它通过internet进行调用,必然存在网络用户都可以调用的安全问题。如何实现webservice的访问 权限限制,是使用webservice用户使用面临重要的问题,下文就给两种方案,从浅到深解决上面问题。

二、基于“soapheader” 特性的简单方法

1." soapheader" 概述  

soap 标头提供了一种方法,用于将数据传递到 xml web services 方法或从 xml web services 方法传递数据,条件是该数据不直接与 xml web services 方法的主功能相关。 多数情况下用来传递用户身份验证信息,当然它的作用远不止如此,有待于在实际应用中发掘。

2.soapheader实现用户身份验证代码

using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.services;
using system.web.services.protocols;
namespace usercenter
{
  public class mysoapheader :soapheader
  {
    public string username
    {
      get;
      set;
    }
    public string pwd
    {
      get;
      set;
    }
  }
  /// <summary>
  /// mymath 的摘要说明
  /// </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 mymath : system.web.services.webservice
  {
    public mysoapheader sheader;
    [webmethod]
    public string helloworld()
    {
      return "hello world";
    }
    [webmethod]
    [soapheader("sheader")]
    public string add(int x, int y)
    {
      if (sheader.username == "test" && sheader.pwd == "test")
      {
        return (x + y).tostring();
      }
      else
      {
        return null;
      }
    }
  }
}

3.缺点分析:

(1)服务逻辑和用户权限验证逻辑混和,加大程序理解复杂度。
(2)权限逻辑重用性不高

二、基于“soapextensionattribute” 特性的方法

1.soapextensionattribute与soapextension概述

soapextension和soapextensio。attribute两个类用于控制webservice序列化和反序列化的一般过程,可对webservice进行压缩和日志等功能进行控制.

2.实现代码 

using system;

using system.collections.generic;

using system.linq;

using system.web;

using system.web.services;

using system.web.services.protocols;

namespace xmlclass1.class15.content

{

  [attributeusage(attributetargets.method)]

  public class myextensionattribute : soapextensionattribute

  {

    int _priority = 1;

    public override int priority

    {

      get { return _priority; }

      set { _priority = value; }

    }

    public override type extensiontype

    {

      get { return typeof(myextension); }

    }

  }

  public class myextension : soapextension

  {

    //这个override的方法会被调用四次

    //分别是soapmessagestage beforeserialize,afterserialize,beforedeserialize,afterdeserialize

    public override void processmessage(soapmessage message)

    {

      if (message.stage == soapmessagestage.afterdeserialize)//反序列化之后处理

      {

        bool check = false;

        foreach (soapheader header in message.headers)

        {

          if (header is mysoapheader)

          {

            mysoapheader myheader = (mysoapheader)header;

            if (myheader.name == "admin" || myheader.password == "admin")

            {

              check = true;

              break;

            }

          }

        }

        if (!check)

          throw new soapheaderexception("认证失败", soapexception.clientfaultcode);

      }

    }

    public override object getinitializer(type type)

    {

      return gettype();
       }

    public override object getinitializer(logicalmethodinfo info, soapextensionattribute attribute)

    {

      return null;

    }

    public override void initialize(object initializer)

    {

    }

  }

  public class mysoapheader : soapheader

  {

    string _name;

    string _password;

    public string name

    {

      get { return _name; }

      set { _name = value; }

    }

    public string password

    {

      get { return _password; }

      set { _password = value; }

    }

  }

  /// <summary>

  /// headersoap2 的摘要说明

  /// </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 headersoap2 : system.web.services.webservice

  {

     public mysoapheader header;

    [webmethod]

    [myextensionattribute]

    [soapheader("header", direction = soapheaderdirection.in)]

    public string checkheader()

    {

      //业务逻辑.

      return "something done";

    }

  }
}

以上就是webservice的安全设置全部内容,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网