简单SSO(Single sign-on)的另类实现方式,钩子技术

      在企业信息化过程中,往往企业中会存一多套已经成熟且稳定应用的系统,系统使用人员通常在登录不同的系统时要输入不同的用户名或账号及密码,影响工作效率,同时在不同的系统中切换时非常繁琐.
      针对上述情况,在实际信息化实施中,我们实现了对于没有源码的CS系统一种另类解决方式.
 
      1\建立标准用户信息表,在单独的数据库中;
      2\维护各应用系统的用户信息与标准用户信息做对照;
      3\创建统一验证平台登录界面进行系统验证;
      4\获取欲进入系统的真正用户名\密码信息,使用钩子技术进行传递,完成用户名\密码输入和校验.


      这只是一个简单的原理,根据需要,还可能要增加各系统与统一验证平台之间用户名\密码统一的工作.

      附件有两个例子:有兴趣的朋友可以看一下.
      WinAppHOOK.exe   为集成验证平台原型.
      WinAppMaster.exe   为子系统原型.
      这个原型的功能,可以通过 WinAppHOOK填充 WinAppMaster的文本框和点击WinAppMaster的按钮.

       WinAppHOOK代码
 1using System;
 2using System.Text;
 3using System.Drawing;
 4using System.Collections;
 5using System.ComponentModel;
 6using System.Windows.Forms;
 7using System.Runtime.InteropServices; 
 8
 9namespace WinAppHOOK
10{
11    public partial class frmMain : Form
12    {
13        public frmMain()
14        {
15            InitializeComponent();
16        }

17
18        private void btnSendClick_Click(object sender, EventArgs e)
19        {
20            IntPtr hwnd_win;
21            IntPtr hwnd_button;
22
23            hwnd_win = FindWindow("WindowsForms10.Window.8.app.0.378734a""Main");
24            hwnd_button = FindWindowEx(hwnd_win, new IntPtr(0), "WindowsForms10.BUTTON.app.0.378734a""OK");
25
26            const int BM_CLICK = 0x00F5;
27            Message msg = Message.Create(hwnd_button, BM_CLICK, new IntPtr(0), new IntPtr(0));
28            PostMessage(msg.HWnd, msg.Msg, msg.WParam, msg.LParam); 
29
30        }

31
32        private void btnSendText_Click(object sender, EventArgs e)
33        {
34            const int WM_CHAR = 0x0102;
35            const int WM_GETTEXT = 0x000D;
36            const int WM_SETTEXT = 0x000C;
37            const int WM_CLICK = 0x00F5;
38            string text = "";
39
40            IntPtr ParenthWnd = new IntPtr(0);            
41            IntPtr EdithWnd = new IntPtr(0);
42
43            ParenthWnd = FindWindow("WindowsForms10.Window.8.app.0.378734a""Main");
44
45            //得到User Name这个子窗体,并设置其内容 
46            EdithWnd = FindWindowEx(ParenthWnd, EdithWnd, "WindowsForms10.EDIT.app.0.378734a""");
47            if (!EdithWnd.Equals(IntPtr.Zero))
48            {
49                text = "User";
50                //调用SendMessage方法设置其内容 
51                SendMessage(EdithWnd, WM_SETTEXT, (IntPtr)0, text);
52            }

53
54            //得到Password这个子窗体,并设置其内容 
55            EdithWnd = FindWindowEx(ParenthWnd, EdithWnd, "WindowsForms10.EDIT.app.0.378734a""");
56            if (!EdithWnd.Equals(IntPtr.Zero))
57            {
58                text = "Password";
59                SendMessage(EdithWnd, WM_SETTEXT, (IntPtr)0, text);
60            }
 
61
62        }

63
64        [DllImport("user32.dll")]
65        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
66
67        [DllImport("user32.dll")]
68        public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
69
70        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
71        public static extern IntPtr PostMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
72
73        [DllImport("User32.dll", EntryPoint = "SendMessage")]
74        public static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, string lParam); 
75
76
77    }

78}

WinAppMaster代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WinAppMaster
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Master单击");
        }
    }
}

posted @ 2007-07-05 08:29  禹过天晴  阅读(705)  评论(0编辑  收藏  举报