• Web Service 中的身份验证策略使用自定义SOAP 标题


    自定义SOAP标题可以限制调用服务的用户范围
     1using System;
     2using System.Web;
     3using System.Web.Services;
     4using System.Web.Services.Protocols;
     5
     6[WebService(Namespace = "http://livebaby.cn")]
     7[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
     8public class Service : System.Web.Services.WebService
     9{
    10    public SecurityHeader currentUser;
    11    public Service()
    12    {
    13
    14        //如果使用设计的组件,请取消注释以下行 
    15        //InitializeComponent(); 
    16    }

    17    [WebMethod, SoapHeader("currentUser")]
    18    public string GetResult(string queryString)
    19    {
    20        if(ValidateUser(currentUser.UserName,currentUser.UserPass))
    21        {
    22            return "你发送的字符串是:"+queryString;
    23        }

    24        else
    25            return "对不起:" + currentUser.UserName+",您不是合法的用户!";
    26    }

    27    //检验SOAP HEADER 
    28    private bool ValidateUser(string user, string pass)
    29    {
    30        if (user.Equals("user"&& pass.Equals("user"))
    31            return true;
    32        else
    33            return false;
    34    }

    35}

    36//自定义Soap Header Class
    37public class SecurityHeader : System.Web.Services.Protocols.SoapHeader
    38{
    39    public string UserName;
    40    public string UserPass;
    41}

    下面是客户端的调用
     1using System;
     2using System.Collections.Generic;
     3using System.ComponentModel;
     4using System.Data;
     5using System.Drawing;
     6using System.Text;
     7using System.Windows.Forms;
     8
     9namespace SoapHeader
    10{
    11    public partial class Form1 : Form
    12    {
    13        public Form1()
    14        {
    15            InitializeComponent();
    16        }

    17
    18        private void button_Invoke_Click(object sender, EventArgs e)
    19        {
    20            SoapHeader.localhost.SecurityHeader header = new SoapHeader.localhost.SecurityHeader();
    21            header.UserName = textBox_User.Text;
    22            header.UserPass = textBox_Pass.Text;
    23            SoapHeader.localhost.Service service = new SoapHeader.localhost.Service();
    24            service.SecurityHeaderValue = header;
    25            this.textBox_Output.Text+=service.GetResult(this.textBox_Input.Text)+Environment.NewLine;
    26        }

    27    }

    28}

    29
  • 相关阅读:
    如何理解Stand SPI Dual SPI 和Quad SPI??
    一款很实用的欠压过压保护电路
    AbpZero之企业微信登录(拓展第三方auth授权登录)第三步:需要注意事项
    在AbpZero中hangfire后台作业的使用——开启hangfire
    AbpZero之企业微信登录(拓展第三方auth授权登录)第一步:查看AbpZero的auth第三方登录的底层机制
    在AbpZero中hangfire后台作业的使用——hangfire的调度
    AbpZero后台模块化(1)
    AbpZero之企业微信登录(拓展第三方auth授权登录)第二步:开始逐步实现企业微信登录
    vue基础(入门视频零基础精华总结)
    组件参数校验与非props特性
  • 原文地址:https://www.cnblogs.com/meil/p/727192.html
Copyright © 2020-2023  润新知