• C#调用Web Service时的身份验证


    原理:webservice所在的系统中,在系统域中建立用于登录的软件的用户和密码,软件登录时将用户名、密码和登录的本机的域的名字通过webService的NetworkCredential传递到webService中通过一个方法得到域中的用户、密码和域进行匹配,从而得到是否有权限

    webservice代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    
    using System.Data;
    using System.Net;
    using System.Data.SqlClient;
    
    namespace WebServiceSocket
    {
        /// <summary>
        /// Service1 的摘要说明
        /// </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 Service1 : System.Web.Services.WebService
        {
    
            /// <summary>
            /// 读取数据表数据统一方法
            /// </summary>
            /// <param name="where">查询条件</param>
            /// <param name="tableName">对应数据表名</param>
            /// <param name="Credential">授权凭证</param>
            /// <returns></returns>
            [WebMethod(Description = "功能:[SQL]读取数据方法,返回值DS", EnableSession = false)]
            public DataSet GetTableDataInfo(string where, string tableName, NetworkCredential Credential)
            {
                try
                {
                    string proc = "select * from " + tableName;
                    proc = string.Format(proc, where);
    
                    SqlConnection con = new SqlConnection();
                    DataSet ds = new DataSet();
                    try
                    {
                        con = new SqlConnection("server=192.168.*.***;database=****;uid=sa;pwd=****");
                        con.Open();
    
    
                        SqlDataAdapter da = new SqlDataAdapter(proc, con);
                        da.SelectCommand.CommandTimeout = 1800;
                        da.Fill(ds);
                    }
                    catch
                    {
                        ds = null;
                    }
                    finally
                    {
                        con.Close();
                    }
                    return ds;
                }
                catch { return null; }//, NetworkCredential Credential
            }
        }
    }

    调用代码:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Net.Sockets;
    using System.Net;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.IO;
    using System.Runtime.Serialization;
    
    namespace clientWinform
    {
        public partial class Form1 : Form
        {
     
            public Form1()
            {
                InitializeComponent();
            }
    private void button2_Click(object sender, EventArgs e)
            {
                //ServiceSocket.Service1SoapClient so = new ServiceSocket.Service1SoapClient();
                //DataSet ds = so.GetTableDataInfo("", "[LYPMMISDB].[Zeda].[tblist]", Credential);
                //dataGridView1.DataSource = ds.Tables[0];
    
                WebReference.Service1 de = new WebReference.Service1();
                WebReference.NetworkCredential app = new WebReference.NetworkCredential();
                app.UserName = "";
                app.Password = "";
                DataSet ds = de.GetTableDataInfo("", "****", app);
                dataGridView1.DataSource = ds.Tables[0];
            }
    
    
        }
    }

     截图:

  • 相关阅读:
    一步一步写数据结构(线索二叉树)
    Android studio 下JNI编程实例并生成so库
    IOS和Android音频开发总结
    IDEA常用快捷键
    Spring事务简介
    IDEA新建Springboot项目
    140201126杨鹏飞作业六
    140201126杨鹏飞作业三
    140201126杨鹏飞作业七
    自我介绍
  • 原文地址:https://www.cnblogs.com/hongmaju/p/4741623.html
Copyright © 2020-2023  润新知